我的问题类似于this one https://stackoverflow.com/questions/1702837/request-isajaxrequest-returns-false我遇到了类似的问题,但一个很大的区别是我使用的是 Ajax 辅助方法(Ajax.ActionLink
and Ajax.BeginForm
) 而不是使用 jQuery 处理 AJAX。
Request.IsAjaxRequest()
对于接受 http GET 的编辑方法返回 true,但对于接受 http POST 的编辑方法返回 false。
GET 请求来自以下生成的链接:
<%=Ajax.ActionLink(item.Name, "Edit", "Device",
new { id = item.ID },
new AjaxOptions { HttpMethod= "GET", UpdateTargetId = "ModalDialog" },
new { name = item.Name })%>
POST 请求来自此代码生成的表单:
<% using (Ajax.BeginForm("Edit", "Device", new { id = Model.ID }, new AjaxOptions { OnComplete = "CloseDialog" }))
{ %>
<fieldset>
<h4>
<label for="Name">Name</label>
</h4>
<%= Html.TextBox("Name", null, new { @class = "required" })%>
<h4>
<input type="checkbox" id="IsActive" name="IsActive" <% if (Model.IsActive)%> <%=Html.Encode("checked=''")%> />
<label for="IsActive">Unit Is Active</label>
</h4>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
这是设计使然吗?我做错了什么吗?我该如何解决这个问题?