ASP.NET MVC CheckBoxList from model with List Property

前端 未结 4 1455
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 07:01

Apologies if the title is unclear.

I\'m trying to return my model from a form submit in ASP.NET MVC.

My question is nearly the same as this question, only di

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 07:39

    No need to go away from Razor at all.

    This works for me:

    for (var i = 0; i < Model.UserRoles.Count(); i++)
    {
        var role = Model.UserRoles[i];
        @Html.HiddenFor(model => model.UserRoles[i].RoleId)
        @Html.CheckBoxFor(model => model.UserRoles[i].Selected)
        @Html.LabelFor(model=> model.UserRoles[i].Name, role.Name)
    }
    

提交回复
热议问题