2

I have a View which allows editing a List of ViewModel. The code works till the displaying functionality. But I am unable to get the value in the same list back from the view.

        public ActionResult Funtion()
        {
            List<ViewModel> ViewModel= (List<ViewModel>)TempData["ViewModel"];
            return View(ViewModel);
        }

        [HttpPost]
        public ActionResult UserFormSubmission(List<ViewModel> ViewModel)
        {    
            return View();
        }        

The value in List of the POST function is null. How could i get the values from view into Controller.

EDIT : I tried using the solutions mentioned to assolution to the question How to pass IEnumerable list to controller in MVC including checkbox state?. But encountered the following error:

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable

Also i couldnt understand the usage of certain tags in it, like the AntiForgeryToken, HiddenFor, etc. So if possible someone please come up with a Simple solution or a better explanation.

EDIT 2 : I changed @model IEnumerable<ViewModel> to @model List<ViewModel> and the error Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable has been corrected.

EDIT 3: I was able to get the value of Textbox and CheckBox into the Controller but RadioButton and DropDownList's selected values cant be identified at the[HttpPost]` function? What am I missing here?

@for (i = 0; i < Model.Count(); ++i)
{
 if (Model[i].TypeId == 4)
                    {
                        //radiobutton
                        for (int j = 0; j < Model[i].Options.Count(); ++j)
                        {
                    <div>
                        @Html.HiddenFor(m => m[i].Options[j].OptionId)                        
                        @Html.HiddenFor(m => m[i].Options[j].OptionValue)
                        @Html.HiddenFor(m => m[i].Options[j].Id)

                        @Html.RadioButtonFor(m => m[i].Options[j].Value, @Model[i].Options[j].DisplayText)
                        @Html.DisplayFor(m => m[i].Options[j].DisplayText)
                    </div>

                        }
                    }
}
Community
  • 1
  • 1
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
  • Please, add more codes about view, for example where is
    html tag?
    – Elvin Mammadov Sep 09 '13 at 09:26
  • 2
    http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx – Vladimir Sep 09 '13 at 09:26
  • @ElvinArzumanoğlu - That's irrelevant on the current situation. I have included only what is needed. – Rohit Vipin Mathews Sep 09 '13 at 09:30
  • If you have a new question, post a new question. Don't keep updating your existing one with independent problems as you'll end up with answers to different questions in the same list. You've solved your problem - your model is binding. Your problem with your RadioButtons and DropDownLists is separate. – Ant P Sep 19 '13 at 11:00
  • @AntP - This is the same question. Thats why Its updated. – Rohit Vipin Mathews Sep 19 '13 at 11:14
  • @Rohit No, it's a different question. Your original question was a problem with view model binding, which now works. Problem solved. Your next question is that some specific properties aren't binding. That's a different question. Meaning all of a sudden all of the existing - correct - answers no longer solve the problem and this Q&A is now a mess that is of now use to anyone. Instead of creating one massive monster question with 20 sub questions, create a new question with just the relevant information. – Ant P Sep 19 '13 at 11:18
  • Its still all about viewmodel binding. And as you could see there is no specific answer. Its all References or how to go about it type. So I don't find a need for a new question yet. Maybe a better answer would come up that solves all the scenarios. – Rohit Vipin Mathews Sep 19 '13 at 11:24
  • A better question would elicit better answers. – Ant P Sep 19 '13 at 15:33

3 Answers3

4

I'd suggest to:

  1. Use strongly typed view
  2. Use for(i = 0; ...; ...) loops, else it will not work this simply

Another approach could be (please don't use this approach):

  1. Create JSON array of the list on JS post event
  2. Send an AJAX POST request to the controller with this data
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
1

You need to use the indexer istead of foreach statment to allow mvc to build your list back. The simmilar question has already been asked How to pass IEnumerable list to controller in MVC including checkbox state?

Community
  • 1
  • 1
Alexandr Mihalciuc
  • 2,537
  • 15
  • 12
1

as the #theghostofc suggested you can use index based loop and controls using for to generate controls e.g.

for(int i=0;i<Model.Count;i++)
{
<tr>
<td>
  @Html.TextBoxFor(model=>Model[i].Text)
</td>
<tr>    
}

And on form submission as you used the list of Models you can obtain it easily.

serene
  • 685
  • 6
  • 16
  • The problem is i Have more thane one type like TextBox, CheckBox,RadioButton,DropDownList,etc – Rohit Vipin Mathews Sep 10 '13 at 04:00
  • you mean u have more than one type of control for same Property.? But if you mean you have other controls like checkbox or radiobutton or dropdownlist for different properties, then you have no problem on obtainning the selected values of these controls as well by using control with for tags as i have exaplined for textbox, but suitable datatype like Not Nullable boolean is required for checkbox and so on.. – serene Sep 11 '13 at 05:53