MVC Processing Index of collection elements in a partial view model

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?

+3
2

, , !

MVC.NET :

  • EditorTemplate PartialView... .

  • define TemplateInfo.HtmlFieldPrefix

, :

@{ 
  Html.RenderPartial("TargetAudienceParameters", Model.TargetAudienceParameter, 
      new ViewDataDictionary {
           TemplateInfo = new TemplateInfo {
               HtmlFieldPrefix = "Advertisements[4]" // 4 being an example
           } 
      }); 
 }
+9

, , ajax, . , . . Smth :

function eduFacilityTableSave() {
    $('#eduFacilityTable tr input[type="text"],#eduFacilityTable tr select').change(function () {
        var parId = $(this).parent().parent().attr('id');
        var parentName = parId.replace(/\d+/g, '');
        var parentId = parId.replace(/\D+/g, '');
        var id = $(this).attr('id');
        var text = $(this).val();
        //alert(text);
        $.ajax({
            type: "POST",
            //contentType: "application/json; charset=utf-8",
            url: "/Form/SaveFromForm/",
            data: { id: id, parentId: parentId, parentName: parentName, text: text },
            dataType: "json",
            success: function (data) {
                //
            }
        });
    });
}

public JsonResult SaveFromForm(string id, string parentId, string parentName, string text) {
            if (parentName == "eduFacility") {
                var edufacility = _repository.GetEduFacilityById(Convert.ToInt32(parentId));
                if (id == "EntryYearId") {
                    edufacility.EntryYearId = Convert.ToInt32(text);
                }
                if (id == "FinishYearId") {
                    edufacility.FinishYearId = Convert.ToInt32(text);
                }
                if (id == "FullName") {
                    edufacility.FullName = text;
                }
                if (id == "Speciality") {
                    edufacility.Speciality = text;
                }
                if (ModelState.IsValid) {
                    _repository.UpdateEduFacility(edufacility);
                    _repository.Save();
                }
            }
            return Json("Success", JsonRequestBehavior.AllowGet);
        }
+1

All Articles