I have a ViewModel with a collection inside, for example:
public class CreateCampaignViewModel : ControlPanelViewModel
{
[RequiredField]
public string Name { get; set; }
public List<AdvertisementViewModel> Advertisements { get; set; }
...
}
in the view, if I use it like:
@for (int i = 0; i < Model.Advertisements.Count; i++)
{
<fieldset style="z-index: 0;">
<legend>מודעה</legend>
<table style="width: 100%;">
<tr>
<td>
<label for="@Html.NameFor(m => m.Advertisements[i].Title)">כותרת הפרסומת</label></td>
<td>@Html.TextBoxFor(m => m.Advertisements[i].Title)</td>
</tr>
</table>
</fieldset>
}
everything is fine and i see the list when i submit the form ...
but I want to insert an ad editor in a partial view, so my view will look like this:
@for (int i = 0; i < Model.Advertisements.Count; i++)
{
Html.RenderPartial("AdvertisementEditor", Model.Advertisements[i]);
}
and my Partial:
@model Pushed.Models.AdvertisementViewModel
<fieldset style="z-index: 0;">
<legend>מודעה</legend>
<table style="width: 100%;">
<tr>
<td>
<label for="@Html.NameFor(m => m.Title)">כותרת הפרסומת</label></td>
<td>@Html.TextBoxFor(m => m.Title)</td>
</tr>
</table>
</fieldset>
My problem is that now when I submit the form, I don’t get the value in the text box for each ad.
This is logical because now the tags <input>do not have the name 'advert [1] .Title', but only the 'Title', which is not in my containing model.
How can I create a partial one that reflects one element of the collection that it works with so that it displays the form correctly?