MVC - 传递价值,通过JS控制器 [英] MVC - Pass value to controller via JS
本文介绍了MVC - 传递价值,通过JS控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个下拉
<%=Html.DropDownList("genre", Model.genres, "", new { @onchange = ChangeMovie()" })%>
JavaScript的样子(不完整)
The JavaScript looks like (incomplete)
function ChangeMovie() {
var genreSelection = container.find( '#genre' ).val();
$.ajax({
"url" : "/Movies/GetGenre" ,
"type" : "get" ,
"dataType" : "json" ,
"data" : { "selectedValue" : $( "#genre").val() },
"success" : function (data) {}
});
};
Conrtroller code
Conrtroller code
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函数控制器code中的作用的结果。我需要帮助,所以正确的值传递到控制器操纵的JavaScript code和AJAX调用code。
I want to pass the selected value of the drop down to the action result in the controller code via the js function. I need help manipulating the JavaScript code and the AJAX call code so the correct value is passed onto the controller.
推荐答案
您有很多不必要的报价也不会在你的行动返回的JSON
You have a lot of unnecessary quotes as well not returning JSON in your action
$.ajax({
url: "/Movies/GetGenre/",
dataType: "json",
cache: false,
type: 'GET',
data: {genreName: $("#genre").val() },
success: function (result) {
if(result.Success) {
alert(result.Genre);
}
}
});
另外你的控制器没有返回JSON,修改你的行动,这
Plus your controller isn't returning Json, modify your action to this
public JsonResult GetGenre(string genreName) {
// do what you need to with genreName here
return Json(new { Success = true, Genre = genreName }, JsonRequestBehavior.AllowGet);
}
这篇关于MVC - 传递价值,通过JS控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文