Http Post with duplicate MVC name values

In MVC, I have a form that uses a lot of duplicate information. Basically, they press a button, and another โ€œformโ€ appears (a drop-down list of text fields, etc.) that appears. When they press the submit button, but it all returns with the same name. How can I either make names different in Mail, or be able to drop items into a list?

My code is:

@Html.TextBox("Textbox1", "", new { placeholder = "", size = "77", maxlength = "76" })
@Html.DropDownList("Country", ViewData["CountryOptions"] as SelectList,"Select", new { id = "Country"})</div>
<div class="pad5">
<span class="FieldLabel">Application *</span><span>@Html.DropDownListFor(model => model.ProductsID, Model.HowProductsAreUsedOptions, new { @class = "General", id = "General-1" })
@Html.DropDownListFor(model => model.ApplicationValue, Model.Options, "--Choose One--", new { id = "Option1", style = "display:none" })
</div>

They can be repeated up to 9 times at the time of sending. However this will give me this in Post

FormID=8&Textbox1=This&Country=USA&ProductsID=1&ApplicationValue=2&
Textbox13=Is&Country=Canada&ProductsID=2&ApplicationValue=3&
Textbox14=A&Country=Canada&ProductsID=2&ApplicationValue=1&
Textbox15=Test&Country=Canada&ProductsID=1&ApplicationValue=8&
Textbox16=For&Country=Canada&ProductsID=2&ApplicationValue=1&
Textbox17=Stack&Country=USA&ProductsID=1&ApplicationValue=9&
Textbox18=Overflow&Country=USA&ProductsID=2&ApplicationValue=2

How can I do something in such a way that it can divide them into 7 different sets of values โ€‹โ€‹instead of giving only one of them?

+4
source share
1 answer

, .

, .

public class FooModel
{
    public string Text { get; set; }
    public string Country { get;set;}
    public int ProductsID { get; set; }
    public int ApplicationValue { get; set; }
}

,

public class FooViewModel
{
   public List<FooModel> Foos { get; set; }
}

FooViewModel .

:

@for (var i = 0; i < Model.Foos.Count; i++) 
 {
      ...
      @Html.TextBoxFor(model => Model[i].Text)
      @Html.DropDownListFor(model => Model[i]Country, ViewData["CountryOptions"] as SelectList,"Select")
      @Html.HiddenFor(model => Model[i].ProductsID)
      ...
 }

HiddenFor .

, FooViewModel, .

. :

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

+2

All Articles