开发者

ASP.NET MVC3: 2 forms on page, submit triggers the wrong one

I have 2 forms on the same page. One is a form in a partial view which is rendered via a child action we'll call this PodForm. The second is rendered by the current action.

So my code looks a little like this (please ignore names etc, this is example code):

Form.cshtml

@model MyProject.Models.FormInputModel
@using(Html.BeginForm("Form","Main",FormMethod.Post))
{
  @Html.TextBoxFor(x=>x.AField)
  @* Some other fields *@
  <input type="submit"/>
}

<div class="sidebar">
   @Html.Action("PodForm","Pod")
</div>

PodForm.cshtml

@model MyProject.Models.PodFormInputModel
@using(Html.BeginForm("PodForm","Pod",FormMethod.Post))
{
  @Html.TextBoxFor(x=>x.Name)
  @* Some other fields *@
  <input type="submit"/>
}

When I click submit on the Main form, the PodForm action method is triggered. What's going on?

Edit - As requested in the comments:

  1. The generated mark up looks similar to the following.

    <form action="/Main/Form" encoding="multipart/form-data" method="POST">
         <input type="text" name="AField" />
         <input type="submit" />
      </form>
    
     <div class="sidebar">
       <form action="/Pod/PodForm" method="POST">
           <input type="text" name="Name" />
           <input type="submit" />
       </form>
     </div>
    
  2. Controller actions

    The "MainController" (not the actual name) has an action called "Form"

    public MainController : Controller
    {
        public ActionResult Form()
        {
            return View();
        }
    
        [HttpPost]
        public ActionResult Form()
        {
            if(ModelState.IsValid)
            {
              //Save
            }
    
            return View();
        }
    }
    

    The "PodController" (not the actual name) has an action called "PodForm". The Reason the HttpPost does not return View is that doing that from a child action returns just the partial view - not what I want.

        public PodController : Controller
        {
           public ActionResult PodForm()
           {
              return View();
           }
    
           [HttpPost]
           public ActionResult PodForm(PodFormInputModel model)
           {
              if(ModelState.IsValid)
              {
                 //Save the thing   
    
                  return RedirectToAction(Request.HttpReferrer.ToString()).AndFlash("Saved");
              }
    
              return RedirectToAction(Request.HttpReferrer.ToString()).AndFlash("Not saved");
           }
        }
    

UPDATE: I have figured out that the first action method is called but because it returns a View() the view engine is calling the PodForm action method as a Post rather than as a Get whi开发者_JAVA百科ch is triggering the submit logic. Weird.


I have worked out that the Action method for the main form is getting hit first and responding correctly. However, when the view for the main form renders, the child action for the pod form also renders but because the request was a post it renders the HttpPost action method, causing the problem.

I've fixed it by renaming the HttpPost method and changing the PodForm.cshtml to use this new action method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜