0

I have a controller with a viewModel that contains an IEnumerable in it and I have a form which is constructed by a jQuery control.

When I post back to the control I want to collect all the rows of hidden fields that were collected into an IEnumerable.

Here's an example view model:

public class MyViewModel
{
    public IEnumerable<SelectListItem> GeneratedItems { get; set; }
}

In the jQuery I create rows in a table with hidden fields like this:

<input class="hiddenValue" name="' + controlName + '_guid" type="hidden" value="' + guid + '"></input>

And then in the POST method of the controller I receive the posted model.

Here's my problem... if I accept a IEnumerabe then all the guids are collected correctly. But I haven't managed to get it to return a IEnumberable.

I have tried adding two hidden fields and trying something like this:

<input class="hiddenValue" name="' + controlName + '.Text" type="hidden" value="' + text + '"></input>

<input class="hiddenValue" name="' + controlName + '.Value" type="hidden" value="' + guid + '"></input>

But that returns an empty array.

Can someone help me arrange the hidden fields so that it will give me a list of the object I want?

Many thanks

Nick
  • 2,877
  • 2
  • 33
  • 62

2 Answers2

0

To achieve this, your post controller should like this :

[HttpPost]
public ActionResult YourAction(YourViewModel obj, FormCollection FC)
{
    List<SelectListItem> Items = new List<SelectListItem>();

    foreach(var key in FC.AllKeys.Where(K => K.EndsWith(".Text"))
    {
        SelectListItem SI = new SelectListItem { Text = key , Value = FC[key] };
        Items.Add(SI);
    }

    obj.GeneratedItems = Items.AsEnumerable();

    return View(obj);

}

Hope this will help !!

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
0

Look at Darin Dimitrov example it shows how it must be done in MVC. (example) Main idea is that all inputs will contain index and then model binder fill your IEnumerable automatically.

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • The hidden fields are created with javascript so this method won't work – Nick Sep 20 '12 at 22:58
  • @Nick I understand this, I am taking about view, how it must be done, look at naming convention and reproduce html-markup with jquery, server code will be same. – webdeveloper Sep 20 '12 at 23:09
  • My model contains an enumerable, it's not an enumerable itself. If I loop through it and create hidden fields it produces hidden fields named as simply "Value" and "Text". Posting back using those is not getting picked up by the controller. That's maybe because the model contains many enumerables of SelectListItem so it wouldn't know which ones to populate if they are simply called "Value" and "Text". – Nick Sep 21 '12 at 10:13
  • @Nick If you look at example, you will see how controls are named. As I said before, just type correct names. As I remember: `[0].Text`, `[0].Value`, then `[1].Text` and `[1].Value`. – webdeveloper Sep 21 '12 at 10:34