Skip to content Skip to sidebar Skip to footer

Mvc 3 Edit Data In A Ienumerable Model View

I'm trying to edit a list of items in a strongly-typed razor view. The templates don't give me the option to edit a list of objects in a single view so I merged the List view with

Solution 1:

How do i do it? FormCollection? Viewdata?

None of the above, use the view model:

[HttpPost]
public ActionResult EditPermissoes(IEnumerable<Permissao> model)
{
    // loop through the model and for each item .HasPermissao will contain what you need
}

And inside your view instead of writing some loops use editor templates:

<table>
    <tr>
        <th></th>
        <th>
            Indicador
        </th>
        <th>
            Nome
        </th>
        <th>Descrição</th>
        <th></th>
    </tr>
    @Html.EditorForModel()
</table> 

and inside the corresponding editor template (~/Views/Shared/EditorTemplates/Permissao.cshtml):

@model Permissao
<tr>
    <td>
        @Html.CheckBoxFor(x => x.HasPermissao)
    </td>
    <td>
        @Html.DisplayFor(x => x.TipoP.IndID)
    </td>
    <td>
        @Html.DisplayFor(x => x.TipoP.Nome)
    </td>
    <td>
        @Html.DisplayFor(x => x.TipoP.Descricao)
    </td>
</tr>

Post a Comment for "Mvc 3 Edit Data In A Ienumerable Model View"