所以我有这个 ascx (partialView) 控件 - ControlTemp
我在 ControlTemp 中有一个 ajax.BeginForm,如下所示:
<% using (Ajax.BeginForm("ControlTemp", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divControlTemp" })) {
....
<input type = "submit" />
<%}%>
在我的母版页中,我将这个 partialView 用作
<div id = "divControlTemp"> <% Html.RenderAction("ControlTemp", "Home", null); %></div>
现在的问题是,如果我有一个页面正在使用这个母版页并且该页面执行回发以发布,则此功能也会被触发:
[ActionName("ControlTemp"), AcceptVerbs(HttpVerbs.Post)]
public ActionResult ControlTemp(string URL)
{
...
return PartialView("ControlTemp");
}
注意:即使我使用 Html.BeginForm 而不是 Ajax.BeginForm,上述方法仍然会被触发
正在使用此主页的页面:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="loginTitle" ContentPlaceHolderID="TitleContent" runat="server">
Log On
</asp:Content>
<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>Log On</h2>
<p>
Please enter your username and password. <%= Html.ActionLink("Register", "Register") %> if you don't have an account.
</p>
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
**<% using (Html.BeginForm("LogOn", "Account")) { %>**
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
请参阅上面代码中的 Html.BeginForm... LogOn ActionMethod 正在被触发,但它也在触发另一种形式的 ActionMethod!
另一个人发布了这个问题,但他/她没有解决方案:
POST method called on MVC UserControls as well as their parent views
注意:没有嵌套形式
请您参考如下方法:
为什么不使用 jQuery。按钮代码:
<input type="submit" id="submitButton" />
jQuery 代码:
$(document).ready(function() {
$('#submitButton').click(function() {
$('#divControlTemp').load('Controller/Action/Parameters');
});
});
