1

I pass an IEnumerable to a view which does not bind on an ajax post call.

I constructed an iteration in my View to access some properties:

@model IEnumerable<MvcNPCA.Models.T_Contact_Medium>


@using (Ajax.BeginForm("Edit", new { medid = ViewBag.medid, cid = ViewBag.cid, parentdiv = ViewBag.parentdiv }, new AjaxOptions { UpdateTargetId = @ViewBag.parentdiv }))
    {

    for (int count = 0; count < Model.Count(); count++)
           {
                @Html.HiddenFor(model => model.ElementAt(count).T_Contact_MediumID)
                @Html.HiddenFor(model => model.ElementAt(count).MediumGegeven)
           }
    <table>
    <tr>
        <th>
            Contact
        </th>
        ...
    </tr>

@foreach (var item in Model)
{
    if (item.T_Contact_MediumID == ViewBag.medid)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Contact.Contactid)
        </td>
        <td>
            @Html.EditorFor(modelItem => item.MediumGegeven)
        </td>
    </tr>
    }
    else
    {

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Contact.Contactid)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MediumGegeven)
        </td>
    </tr>
    }
}

</table>
    <input type="submit" value="Save" />
    }

However in my controller there is no model binding:

    [HttpPost]
    public ActionResult Edit(IEnumerable<T_Contact_Medium> t_contact_medium, int medid, int cid, string parentdiv)
    {
        if (ModelState.IsValid)
        {
            _service.UpdateMedium(_service.GetMediumByID(medid));

            ViewBag.cid = cid;
            ViewBag.parentdiv = parentdiv;
            ViewBag.MediumId = new SelectList(_MediumEnumService.GetAll(), "MediumId", "Medium1");
            var tcrmodel = _service.GetMediumByContactID(cid);

            return PartialView("Index",tcrmodel);
        }

        ViewBag.MediumId = new SelectList(_MediumEnumService.GetAll(), "MediumId", "Medium1");
        return PartialView(t_contact_medium);
    }

I think I have the controller part right.
Any ideas on what I might have overlooked?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The problem I had has many possible solutions. In my case I want to build a table from an IEnumerable with one editable row.
So instead of passing the complete IEnumerable model to controller, I could just send the single entity.
The way I did this is by only access the editable entity through the model.

Like so:

@{var count = 0;}

@foreach (var item in Model)
{
    if (item.T_Contact_MediumID == ViewBag.medid)
    {

        @Html.HiddenFor(model => model.ElementAt(count).T_Contact_MediumID)
        @Html.HiddenFor(model => model.ElementAt(count).MediumId)
        @Html.HiddenFor(model => model.ElementAt(count).ContactId)

    <tr>
        <td>
            @Html.DisplayFor(model => model.ElementAt(count).Contact.Voornaam)
        </td>
        <td>
            @Html.DisplayFor(model => model.ElementAt(count).Medium.Medium1)
        </td>
        <td>
            @Html.EditorFor(model => model.ElementAt(count).MediumGegeven)
        </td>s
        <td>
            @Html.EditorFor(model => model.ElementAt(count).Login)
            @*Html.CheckBoxFor(modelItem => item.Login*@
        </td>
        <td>
            @Html.DisplayFor(model => model.ElementAt(count).Gebruikernaam)
        </td>
        <td>
            <input type="submit" value="opslagen" />
        </td>
    </tr>
    }
    else
    {   

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Contact.Voornaam)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Medium.Medium1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MediumGegeven)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Login)
            @*Html.CheckBoxFor(modelItem => item.Login*@
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gebruikernaam)
        </td>
        <td>
            @Ajax.ActionLink("Edit", "Edit", new { medid = item.T_Contact_MediumID, cid = ViewBag.cid, parentdiv = ViewBag.parentdiv }, new AjaxOptions { UpdateTargetId = ViewBag.parentdiv }) |
            @Html.ActionLink("Details", "Details", new { id = item.T_Contact_MediumID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.T_Contact_MediumID })
        </td>
    </tr>

    }

I understood that to serialize a IEnumerable back at the controller, one way of doing this is to wrap your ienumarable in a custom view model.

Steffe
  • 348
  • 3
  • 16
  • If you put a breakpoint on the line `var tcrmodel = _service.GetMediumByContactID(cid);`, does `tcrmodel` hold a collection or is it null? – Adrian Thompson Phillips Apr 16 '12 at 10:33
  • The logic in my controller method is not correct. I should get the entity data through the IEnumerable<>. Just to tackle any misunderstandings, is it common to edit my original posting? – Steffe Apr 16 '12 at 11:23
  • 1
    you can refer to the link below. Might help you. http://stackoverflow.com/questions/9820441/model-returned-to-controller-by-edit-view-is-always-null/9820689#9820689 – Dharmesh Tailor Apr 16 '12 at 11:44
  • Thank you for the link. I've been reading alot, but your link somehow pointed me in the right direction. I will post an answer soon. – Steffe Apr 16 '12 at 13:25

0 Answers0