Sha*_*ean 46 asp.net-mvc jquery
我通过jquery提交了一个表单,但是我需要ActionResult来返回true或false.
这是控制器方法的代码:
[HttpPost]
public ActionResult SetSchedule(FormCollection collection)
{
try
{
// TODO: Add update logic here
return true; //cannot convert bool to actionresult
}
catch
{
return false; //cannot convert bool to actionresult
}
}
Run Code Online (Sandbox Code Playgroud)
我如何设计我的JQuery调用来传递表单数据,并检查返回值是true还是false.如何编辑上面的代码以返回true或false?
Mat*_*son 79
您可以以bool或bool属性的形式返回json结果.像这样的东西:
[HttpPost]
public ActionResult SetSchedule(FormCollection collection)
{
try
{
// TODO: Add update logic here
return Json(true);
}
catch
{
return Json(false);
}
}
Run Code Online (Sandbox Code Playgroud)
恕我直言,您应该使用JsonResult而不是ActionResult(为了代码可维护性)。
在Jquery端处理响应:
$.getJSON(
'/MyDear/Action',
{
MyFormParam: $('MyParamSelector').val(),
AnotherFormParam: $('AnotherParamSelector').val(),
},
function(data) {
if (data) {
// Do this please...
}
});
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你 : )
| 归档时间: |
|
| 查看次数: |
27882 次 |
| 最近记录: |