我学习 ASP.NET MVC 并且遇到了一些问题。
我在视图中通过 ajax 辅助方法创建了两个链接。他们发送 mailsType 参数和响应 json 数据。这是链接和 AjaxOptions 对象的代码
<div class="list-group" style = "text-align: center; margin-top: 10px;">
@Ajax.ActionLink("Incoming", "GetMails", new { mailsType = State.Incoming }, opts,
new{@class = "list-group-item active", data_type = "Incoming"})
@Ajax.ActionLink("Outgoing", "GetMails", new {mailsType = State.Outgoing}, opts,
new { @class = "list-group-item", data_type = "Outgoing" })
</div>
这是 ajaxoption,在 ajax 助手中使用
AjaxOptions opts = new AjaxOptions
{
OnSuccess = "viewMail",
};
我通过javascript函数处理响应
function viewMail(data) {
var html =
'<table class="table table-hover table-striped">' +
'<thead>' +
'<tr><th>' + (data.State == 'Incoming' ? 'From' : 'To') + '</th> <th>Subject</th> <th>Date</th></tr>' +
'</thead>' +
'<tbody>';
$(data.Mails).each(function(i, el) {
html += '<tr><td>' + el.From + '</td> <td>' + el.Subject + '</td> <td>' + el.Date + '</td></tr>';
});
html += '</tbody></table>';
$('#contentDiv').html(html);
}
这是控制器中的操作方法代码
public ActionResult GetMails(State mailsType)
{
if (Request.IsAjaxRequest())
{
return Json(new
{
State = Enum.GetName(typeof (State), mailsType),
Mails = _serviceManager.GetMessages(mailsType)
}, "application/json", JsonRequestBehavior.AllowGet);
}
else
{
ViewBag.State = Enum.GetName(typeof(State), mailsType);
return PartialView(_serviceManager.GetMessages(mailsType));
}
}
当我使用第二个链接时一切正常,但是当我使用第一个链接时,我有内部服务器错误,代码为 500,响应类型为 text/html。我使用此链接返回部分视图,它们以前都可以使用。
我尝试使用自己的 ajax 请求,但结果仍然相同。我不明白出了什么问题。我不明白这个问题是抽象的,但可能是你有同样的问题或知道它为什么会发生。
编辑1
当我从地址栏中的链接写入地址时,出现此错误:System.InvalidOperationException:此流不支持超时。
我尝试在 ajax 请求中设置大超时,但没有帮助。当我发送较少的数据作为响应时,请求成功执行。我认为这与响应发送的数据大小有关。我错了吗?这个错误的原因可能是什么?
我会很高兴你的所有建议。谢谢大家!