开发者

ASP.NET MVC - RenderAction - PartialView - BeginForm() - multiple forms

So I have this ascx (partialView) control - ControlTemp

I have an ajax.BeginForm inside ControlTemp like this:

<% using (Ajax.BeginForm("ControlTemp",  new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divControlTemp" })) {
        ....
        <input type = "submit"  />
        <%}%>

Inside my masterpage, I am using this partialView as

<div id = "divControlTemp">  <% Html.RenderAction("ControlTemp", "Home", null); %></div>

Now the problem is if I have a page that is using this master page and the page does a postback to post, this function is also being fired:

[ActionName("ControlTemp"), AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ControlTemp(string URL)
        {
            ...
            return PartialView("ControlTemp");
        }

NOTE: Even if I use Html.BeginForm instead of Ajax.BeginForm, the above methods still ends up being triggered

The page that is using this masterpage:

<%@ 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>
<% } %>

开发者_开发百科See the Html.BeginForm in above code... The LogOn ActionMethod is being fired, but it is firing ActionMethod of this another form as well!

Another person posted this problem, but s/he did not have a solution:

POST method called on MVC UserControls as well as their parent views

Note: There are no nested forms


Why not use jQuery. Code for your button:

<input type="submit" id="submitButton"  />

jQuery code:

$(document).ready(function() {
    $('#submitButton').click(function() {
        $('#divControlTemp').load('Controller/Action/Parameters');
    });
});


The problem is that MVC replicates HTTP method from parent view for every RenderAction() call in it no matter whether its form has actually issued a postback or not.

You will have to create your own functionality the way that I did. I've rewritten this so I can call Html.RenderAction(HttpVerb.Get, ... ). This makes it possible to always render some action as GET, no matter whether containing page is a POST call. In your case this would solve the problem, because rendering the partial view (with ajax form) on your master should always be called as GET. You Ajax makes POSTs anyway.


Post action should return same result for post an get by fallowing code:

[ActionName("ControlTemp"), AcceptVerbs(HttpVerbs.Post)] 
        public ActionResult ControlTemp(string URL) 
        { 
            if(this.ControllerContext.IsChildAction)
                 return  ControlTemp()//Get action method
            ... 
            return PartialView("ControlTemp"); 
        } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜