Jac*_*ack 2 ajax asp.net-mvc json asp.net-mvc-partialview asp.net-mvc-4
在一个 MVC5 项目中,我打开一个模态对话框,如果出现异常,我想打开这个对话框并在这个对话框的 div 上显示一条消息。据我所知,我应该遵循将 partialview 渲染为字符串的方法,但大多数示例在 MVC5 中都不起作用,因为在Return Partial View 和 JSON from ASP.NET MVC Action 上。是否有任何类似或更好的方法适用于 MVC5?
您可以执行以下操作
解决方案 1(使用局部视图)
[HttpPost]
public ActionResult YourAction(YourModel model)
{
if(model!=null && ModelState.IsValid)
{
// do your staff here
Response.StatusCode = 200; // OK
return PartialView("ActionCompleted");
}
else
{
Response.StatusCode = 400; // bad request
// ModelState.ToErrors() : is an extension method that convert
// the model state errors to dictionary
return PartialView("_Error",ModelState.ToErrors());
}
}
Run Code Online (Sandbox Code Playgroud)
您的部分视图应如下所示:
<div id="detailId">
<!-- Your partial details goes here -->
....
<button type="submit" form="" value="Submit">Submit</button>
</div>
Run Code Online (Sandbox Code Playgroud)
你的剧本
<script>
$(document).ready(function(){
$('#formId').off('submit').on('submit', function(e){
e.preventDefault();
e.stopPropagation();
var form = $('#formId');
$.ajax({
url: form.attr('action'),
data: form.serialize(),
method: 'post',
success : function(result){
$('#detailId').replaceWith(result);
// another option you can close the modal and refresh your data.
},
error: function(data, status, err){
if(data.status == 400){
$('#detailId').replaceWith(data.responseText);
}
}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
解决方案 2(使用 Json)
在你的行动中
[HttpPost]
public ActionResult YourAction(YourModel model)
{
if(model!=null && ModelState.IsValid)
{
// do your staff here
return Json(new {status = 200,
//...any data goes here... for example url to redirect
url=Url.Content("YourRedirectAction","Controller")},
}
else
{
return Json( new {status= 400,errors = ModelState.ToErrors()});
}
}
Run Code Online (Sandbox Code Playgroud)
你的脚本应该看起来像
<script>
$(document).ready(function(){
$('#formId').off('submit').on('submit', function(e){
e.preventDefault();
e.stopPropagation();
var form = $('#formId');
$.ajax({
url: form.attr('action'),
data: form.serialize(),
method: 'post',
success : function(result){
if(result.status==200) { // OK
// you might load another action or to redirect
// this conditions can be passed by the Json object
}
else{ // 400 bad request
// you can use the following toastr based on your comment
// http://codeseven.github.io/toastr/demo.html
var ul = $('<ul>')
for(var error in result.errors)
{
ul.append('<li><b>' + error.Key + '</b>:' + error.Value + '</li>;
}
toastr["warning"](ul[0].outerHTML);
}
}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
最后,如果你想要扩展 ModelState.ToErrors()
public static IEnumerable ToErrors(this ModelStateDictionary modelState)
{
if (!modelState.IsValid)
{
return modelState.ToDictionary(kvp => kvp.Key,
kvp => kvp.Value.Errors
.Select(e => e.ErrorMessage).First())
.Where(m => m.Value.Count() > 0);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮到你
| 归档时间: |
|
| 查看次数: |
5924 次 |
| 最近记录: |