Actionメソッドのパラメータ名が、の場合id
、
public ActionResult output(string id)
{
//do something
}
次に、このようなフォームアクションURLを使用できます(デフォルトのルーティングが残りを処理します)
/Shops/output/somestringhere.
別の名前を使用している場合は、それをクエリ文字列として使用します
public ActionResult output(string name)
{
//do something
}
次に、次のようなフォームアクションURLを使用します
/Shops/output?name=somestringhere
コードに関するもう1つの提案は、ドロップダウンをレンダリングするためにViewdataを回避することです。強く型付けされたビューモデルと、データをビューに転送するためのプロパティを使用してみてください。また、JavaScriptをビューから移動して、邪魔にならないようにしてください。ビューがクリーンなマークアップとしてのみ保持されるようにします。
ドキュメント作成ビューにリビジョンドロップダウンを表示する場合は、ビューモデルにプロパティを追加してドロップダウンアイテムを作成します。
public class DocumentCreateViewModel
{
//Other properties also here
public List<SelectListItem> Revisions{ set;get;}
public int SelectedRevision { set;get;}
public DocumentCreateViewModel()
{
Revisions=new List<SelectListItem>();
}
}
GETアクションで、ドロップダウンコンテンツをRevisionsプロパティに入力します。
public ActionResult Create()
{
var vm=new DocumentCreateViewModel();
vm.Revisions=GetRevisionItemsFromSomeWhere();
return View(vm);
}
そして、あなたの強くタイプされた見方では、
@model DocumentCreateViewModel
@using(Html.Beginform())
{
@Html.DropDownListFor(x => x.SelectedRevision,
new SelectList(Model.Revisions,"Value","Text"), "Select..")
<input type="submit" />
}
ドロップダウンの変更イベントでフォーム送信を処理するために、このスクリプトを追加します。
$(function(){
$("#SelectedRevision").change(function(){
var _this=$(this);
var selectedRevision=_this.val();
$("form#YourFormIDHere")
.attr("action","./Shops/output/"+selectedRevision).submit();
});
});
URLをshops/outputにハードコーディングする代わりに、かみそりヘルパーメソッド(@Url.Action
)を使用して適切なパスを取得できます。