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>
}
}
}