tags:

views:

362

answers:

3

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" %>

Log On

Log On

Please enter your username and password. <%= Html.ActionLink("Register", "Register") %> if you don't have an account.

<%= 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:

http://stackoverflow.com/questions/523695/post-method-called-on-mvc-usercontrols-as-well-as-their-parent-views

Note: There are no nested forms

+1  A: 

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');
    });
});
Martin
that is not a proper answer, it is just a workaround.
progtick
As far as code goes, this won't solve the problem. Regular postbacks will always POST all other internal RenderActions...
Robert Koritnik
A: 

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.

Robert Koritnik
A: 

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"); 
        } 
mehran