3

ドロップダウンがあります

 <%=Html.DropDownList("genre", Model.genres, "", new { @onchange = ChangeMovie()" })%>

JavaScriptは(不完全)のように見えます

function ChangeMovie() {
    var genreSelection = container.find( '#genre' ).val();

    $.ajax({  
        "url" : "/Movies/GetGenre" ,
        "type" : "get" ,
        "dataType" : "json" ,
        "data" : { "selectedValue" : $( "#genre").val() },
        "success" : function (data) {}
    });
};

コントローラーコード

public ActionResult GetGenre(string genreName)
{
   //Need to get the `genreName` from the JavaScript function above.. which is
   // in turn coming from the selected value of the drop down in the beginning.

}

ドロップダウンの選択した値を、js関数を介してコントローラーコードのアクション結果に渡したいです。正しい値がコントローラーに渡されるように、JavaScriptコードとAJAX呼び出しコードを操作するのに助けが必要です。

4

3 に答える 3

3

Ajaxリクエストに投稿している値のパラメーター名がActionパラメーター名と一致しません。selectedValuegenreNameである必要があります。

これを変える:

"data" : { "selectedValue" : $( "#genre").val() },

これに:

data : { genreName : $("#genre").val() },
于 2012-05-21T14:59:31.000 に答える
3

モデルバインディングが正しく機能するには、渡されたjsonオブジェクトのフィールド名がコントローラーアクションのパラメーター名と一致する必要があります。だから、これはうまくいくはずです

"data" : { "genreName" : $( "#genre").val() },
于 2012-05-21T15:00:02.303 に答える
3

アクションでJSONを返さないだけでなく、不要な引用符がたくさんあります

$.ajax({
    url: "/Movies/GetGenre/",
    dataType: "json",
    cache: false,
    type: 'GET',
    data: {genreName: $("#genre").val() },             
    success: function (result) {
        if(result.Success) {
            alert(result.Genre);
        }
    }
});

さらに、コントローラーがJsonを返さない場合は、アクションをこれに変更してください

public JsonResult GetGenre(string genreName) {
    // do what you need to with genreName here 
    return Json(new { Success = true, Genre = genreName }, JsonRequestBehavior.AllowGet);
}
于 2012-05-21T15:06:51.603 に答える