Click here to Skip to main content
15,887,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am dynamically building pages with forms from my db, which means I can have 1 form or many per page.

All of my forms have unique Ids and Submit buttons, however within each form the ids and names of the inputs are the same.

<form action="/" class="form-horizontal" id="form1" method="post" role="form">    <h1>1. What is your favourite colour?</h1>
    <select class="form-control" id="Answer" name="Answer">
          <option value="red">red</option>
          <option value="blue">blue</option>
          <option value="yellow">yellow</option>
          <option value="green" selected>green</option>
          <option value="lavender">lavender</option>
    </select>
    <input type="hidden" name="page" id="page" value="1" />
    <input type="submit" name="form1" class="btn btn-default" value="Save answer" />

<form action="/" class="form-horizontal" id="form2" method="post" role="form">    
<h1>2. Do you own a car?</h1>
    <input type="radio" name="Answer" id="Answer" value="yes" />yes               
    <input type="radio" name="Answer" id="Answer" value="no" />no                
    <input type="radio" name="Answer" id="Answer" value="maybe!" />maybe!  
<input type="hidden" name="page" id="page" value="1" />
<input type="submit" name="form2" class="btn btn-default" value="Save answer" />

My post action looks like this;
[HttpPost]
 public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel)
  {
     var page = System.Web.HttpContext.Current.Request["page"];

In this example, instead of receiving "1" for "page" variable, I am getting "1,1".

Is there a way I can bind to the value from the ONE form that is being submitted?
Posted

AFAIK, there is no way to submit one form. If you submit any form from the browser, it will submit all of them. In MVC, you'll get a FormsCollection[^] passed to your action method. Normally, you would just pick out the form of interest to your code and ignore the rest in the collection.
 
Share this answer
 
Comments
AlexHarmes 25-Jan-14 11:53am    
Do you have an example of how I could isolate get it from the FormsCollection as I am not at all familiar with its proper use?
Dave Kreskowiak 25-Jan-14 13:13pm    
You mean like this:
[HttpPost]
public ActionResult SubmitFormWithFormCollection(FormCollection formCollection)
{
string firstName = formCollection["firstname"];
}
AlexHarmes 28-Jan-14 6:06am    
No, that part I get - my question is how do I know if it was form1, form2 or form3 that was submitted? I cannot find any distinguishing returns to identify which form the input is coming from.
Dave Kreskowiak 28-Jan-14 9:15am    
Normally, you wouldn't have both forms post to the same action. They would post to different actions. There's your answer.

The only other way around that that I know of is to create a hidden input field in your forms that denotes which form is being submitted. Check the value of that field in your action method before trying to process the form.
Too close to the forest for the trees??? One silly mistake and it has taken me days to sort it; I never closed the form tag... I am not used to HTML helpers and it is obvious how dangerous a little bit of knowledge is.

To submit a single form, close the tag; Html.EndForm();
 
Share this answer
 
I can't believe this - I hadn't used the Html.EndForm(); tag. By inserting this at the end of my View form block, I now have the form returning just the values for the one submitted!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900