1

I have a form in View where are multiple fields. I send them to controller and I want to bind them to List. How can I achieve this?

For now i have this code

View

using (Html.BeginForm("GetVendorInvoice", "Sale", FormMethod.Get))
{
    foreach (var invoice in Model.VendorInvoices)
    { 
        @Html.Hidden("invoiceId", invoice.InvoiceId)
    }
    <input type="submit" value="Wszystkie na raz"/>
}

Controller:

public ActionResult GetVendorInvoice(List<string> invoiceIdList) {
...}

I read some article that said it shoud work but actually it does not. Any sugestions?

4
  • invoiceId list you want? Commented Apr 17, 2014 at 13:00
  • Yes. In Model they are declared as string type Commented Apr 17, 2014 at 13:04
  • The controller code that you put there is the Post or the get? Also what do you want to do in the view with that list? What's not working? Commented Apr 17, 2014 at 13:08
  • Its get. In the view i just want to send list of strings to the controller. This list has variable amount of items. Now, when i send this form to controller the invoiceIdList is null. I checked in view that all hidden fields are correctly populated. Commented Apr 17, 2014 at 13:22

3 Answers 3

2

You have property name InvoiceId while in action you paremeter is invoiceIdList which is wrong and list will be null in the action, do like this in action:

public ActionResult GetVendorInvoice(List<string> InvoiceId) {
    ...}

or you can do like this:

foreach (var invoice in Model.VendorInvoices)
    { 
        @Html.Hidden("InvoiceId", invoice.InvoiceId)
    }

action:

public ActionResult GetVendorInvoice(List<string> invoiceId) {
...}
Sign up to request clarification or add additional context in comments.

Comments

0

Actions:

public virtual ActionResult Index()
{
    return View(new HomepageModel()
    {
            Strings = new[] {"one", "two", "three"}
    });
}

[HttpPost]
public ActionResult Index(List<string> strings)
{
    throw new Exception(String.Join(", ", strings));
}

View:

@using (Html.BeginForm())
{

    for (int i = 0; i < Model.Strings.Length; ++i)
     {
         @Html.HiddenFor(model => model.Strings[i])
     }

    <input type="submit" value="submit" />
}

You should use the HiddenFor helper method instead of Hidden, as this is strongly typed and will ensure your view matches the model. Using the indexer will generate the correct names for the hidden fields.

Comments

0

I heve found the answer. First of all there was an error. Filed name in view was different from parameter name in controller. When i fixed this i have got a string of hidden fields values separated with commas. Now i have to split this string and everithing is fine now. Thanks for Your effort.

1 Comment

yes instead of this List<string> invoiceIdList you need to do List<string> invoiceId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.