使用 jquery 的 ASP.NET MVC 中的异常管理
我有 2 个问题,在第一个问题中,我得到一个列表,我想 C# 代码(控制器)中的 excetpion 是否有可能显示视图(错误视图)并将其显示为特定的 div。如何获取 .error 中的视图? ? HTML
<div><a href="#" class="MnuCustomerList">List</a></div>
jQuery
$(".MnuCustomerList").click(function () {
var jqxhr = $.post("/Customer/List", function (data) {
$('#rightcolumn').html(data);
})
.success(function () { alert("success"); })
.error(function () { alert("error"); })
.complete(function () { alert("complete"); });
})
;
控制器:
public PartialViewResult List()
{
throw new Exception("myexception");
return PartialView("List");
}
第二个问题: 我在控制器中有一个表单
@model MyModel
@using (Html.BeginForm("Save", "Customer", FormMethod.Post))
{
<table style="width:100%;">
<tr>
<td>Code</td>
<td>@Html.TextBoxFor(m => m.Customer.Code, new { id = "tbCode" })</td>
</tr>
<tr>
<td>LastName</td>
<td>@Html.TextBoxFor(m => m.Customer.LastName, new { id = "tb", maxlength = 50, style = "width:40%;" })</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="A submit button"/></td>
</tr>
</table>
}
,我检查代码是否已经存在,如果是的话 CustomerException(“代码存在”)。
我的问题是,是否可以使用 jQuery 发布此表单,但仍使用此模型的格式,而不是像下面的示例那样逐一获取值
$.ajax({
type: "POST",
url: "/Customer/Save",
data: {
id: $('#Id').val(),
firstName: $('#FirstName').val(),
lastName: $('#LastName').val(),
isEnable: $('#IsEnable').attr('checked')
},
success: function (html) {
$("#ContentDataSection").html(html);
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }
});
谢谢,
I have 2 question, in the first one, I get a list, I'd like if excetpion in C# code (controller) has the possibility to a View (an error view) and display it a specific div. How can I get the view in the .error. ?
HTML
<div><a href="#" class="MnuCustomerList">List</a></div>
jQuery
$(".MnuCustomerList").click(function () {
var jqxhr = $.post("/Customer/List", function (data) {
$('#rightcolumn').html(data);
})
.success(function () { alert("success"); })
.error(function () { alert("error"); })
.complete(function () { alert("complete"); });
})
;
Controller:
public PartialViewResult List()
{
throw new Exception("myexception");
return PartialView("List");
}
Second question :
I have a form
@model MyModel
@using (Html.BeginForm("Save", "Customer", FormMethod.Post))
{
<table style="width:100%;">
<tr>
<td>Code</td>
<td>@Html.TextBoxFor(m => m.Customer.Code, new { id = "tbCode" })</td>
</tr>
<tr>
<td>LastName</td>
<td>@Html.TextBoxFor(m => m.Customer.LastName, new { id = "tb", maxlength = 50, style = "width:40%;" })</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="A submit button"/></td>
</tr>
</table>
}
In the Controller, I check if the code already exist, if yes CustomerException("Code exist").
My questions, is it possible to post this form using jQuery BUT still using this format that's mean with the model and NOT get one value by one value like the sample below
$.ajax({
type: "POST",
url: "/Customer/Save",
data: {
id: $('#Id').val(),
firstName: $('#FirstName').val(),
lastName: $('#LastName').val(),
isEnable: $('#IsEnable').attr('checked')
},
success: function (html) {
$("#ContentDataSection").html(html);
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }
});
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当发出 ajax 请求时出现错误时,才会触发错误回调。如果服务器端发生错误,您需要将数据(Json 格式是一个不错的选择)传递回客户端,指示服务器端出现故障,并在成功回调中处理它。
编辑以添加显示如何将响应代码设置为 500 的代码,以便可以根据 Ryan 的评论在错误回调中处理它:
控制器操作:
Javascript:
The error callback is only going to fire if there is an error in making the ajax request. If an error occurs server side, you'll need to pass data back (in Json format would be a good choice) to the client indicating there was a failure server side and handle it in the success callback.
Edit to add code showing how to set response code to 500 so it can be handled in the error call back per Ryan's comment:
Controller action:
Javascript: