MVC 3 Edit data in a IEnumerable Model View

后端 未结 1 732
不知归路
不知归路 2021-01-05 00:35

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 v

相关标签:
1条回答
  • 2021-01-05 01:10

    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>
    
    0 讨论(0)
提交回复
热议问题