3

I am working with .net MVC 4 my problem is I have multiple input control with same name with postfix 0,1,2 like array. I want all of those values in one object or list of object. Controls in my View(Razor,.cshtml) is like below:

<input type="hidden" value="FirstName" name="MyListData[0]" id="MyListDataUnSelected_0_">
<input type="hidden" value="MiddleName" name="MyListData[1]" id="MyListDataUnSelected_1_">
<input type="hidden" value="LastName" name="MyListData[2]" id="MyListDataUnSelected_2_">

and my post method on controller is like:

public ActionResult Index(List<string> MyListData, FormCollection pFormCollection)

in from collection it gives me value of MyListData[0],MyListData[1],... but List MyListData is null. I have one other Page in that same code is applied and in that case I get list of values in MyListData.

I also tried using string[] MyListData but still not work.

How can I get that list in model or from Form Collection.

6
  • Just a wild guess. What happens if you remove the pFormCollection argument from the action, and leave only MyListData? Commented Feb 6, 2015 at 6:47
  • Do you generate your inputs through razor helper @Html.TextBoxFor<>? Commented Feb 6, 2015 at 7:12
  • No, I only used <input> controls because those are created using jquery. Commented Feb 6, 2015 at 8:29
  • @DixonD if I remove pFormCollection then also MyListData is null only. Commented Feb 6, 2015 at 8:32
  • 1
    Using Firebug or similar, are you able to observe that the values are actually posted? You may also want to try installing Glimpse. Commented Feb 6, 2015 at 8:39

1 Answer 1

3

My current solution is

List<string> lststr=new List<string>();
int i=0;
while(true)
{
    if(pFormCollection["MyListData[" + i + "]"]!=null)
    {
         lststr.add(pFormCollection["MyListData[" + i + "]"]);
         i++;
    }
    else
        break;
}

or in short

List<string> lststr=new List<string>();
int i=0;
while(pFormCollection["MyListData[" + i + "]"]!=null)
{

         lststr.add(pFormCollection["MyListData[" + i + "]"]);
         i++;

}
Sign up to request clarification or add additional context in comments.

Comments

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.