Question

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 which is triggering the submit logic. Weird.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top