0

わかりましたので、コントローラーメソッドを実行してパラメーターを送信Html.DropDownListできるようにしたいと思います。ActionResult output(string test)私はすでにこのようなものを持っていますが、 Uncaught TypeError: Cannot set property 'action' of null メッセージが表示されます:

@Html.DropDownList(
"revisions", ViewData["revisions"] as SelectList,
new
{
    onchange = "this.form.action = '/Shops/output('test')'; this.form.submit();"
})

コードを修正するにはどうすればよいですか?

4

2 に答える 2

1
@Html.DropDownList(
"revisions", ViewData["revisions"] as SelectList,
new
{
    onchange = "submitForm();"
})

そしてあなたのJavacriptはここに行きます

function submitForm()
{
        var form = document.forms[0];
        form = '/Shops/output?test=test'; 
        form.submit();
}
于 2013-03-12T12:31:20.623 に答える
1

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)を使用して適切なパスを取得できます。

于 2013-03-12T12:31:22.020 に答える