Functionality: i want to update viewModel from jquery each time user choose to update PersonList on the view.
So if i have a class person :
public class Person
{
public string Name {get;set;}
public int Age {get;set;}
}
And i have a complex viewmodel which contain list of persons in the view i run :
foreach(var person in model.Item.PersonList)
{
@Html.Partial("_PersonList", person)
}
Partial view :
//_PersonList partial
@model Person
@using (Html.BeginCollectionItem("Item.PersonList"))
{
//hidden fields just for testing
@Html.HiddenFor(x => Model.Name)
@Html.HiddenFor(x => Model.Age )
}
each time user adds new person, i send ajax request and asp for the partialView(_PersonList) which already have to be populated with person data.
Age and name fields inside partial, after ajax request ends like this:
<input data-val="true" data-val-required="" id="item_PersonList_1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36__Name" name="item.PersonList[1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36].Name" type="hidden" value="somRandomName">
this works fine, however if user choose to add 20 person, then i have to do 20 requests to the server, which i want to avoid.
Thats why i tryed to send list of persons to the partialview.
I changed partialView :
//_PersonList partial
@model List<Person>
@using (Html.BeginCollectionItem("Item.PersonList"))
{
@for (var i; i < Model.Count(); i++)
{
//hidden fields just for testing
@Html.HiddenFor(x => Model[i].Name)
@Html.HiddenFor(x => Model[i].Age )
}
}
But now input fields after i get the list looks like this :
<input data-val="true" data-val-required="" id="item_PersonList_1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36_0_Name" name="item.PersonList[1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36][0].Name" type="hidden" value="somRandomName">
Which is not recognized as a part of viewmodel when i post form. so the problem is that there is added a [0] from nowhere to the name and id attributes
how can i prevent this behavior?
If i use foreach id looks like:
item.Products[4456a32b-eb06-4665-be23-31666d72824e].item.Uid