【问题标题】:Foreach ViewModel with nested classes to update Checkbox List具有嵌套类的 Foreach ViewModel 以更新复选框列表
【发布时间】:2012-11-16 00:57:35
【问题描述】:

我需要根据以下 ViewModel 更新复选框列表:

namespace ViewModels
{
    public class MemberProfileListViewModel
    {
        public IList<MemberProfileDetailViewModel> MemberProfileDetails { get; set; }
    }

    public class MemberProfileDetailViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool Selected { get; set; }
    }
}

观点:

@model ViewModels.MemberProfileListViewModel

@using (Html.BeginForm()) {

    <fieldset>

    @foreach (var item in ??????)
    {
        <p>@Html.CheckBoxFor(????)</p>
    }
    <p>
        <input type="submit" value="Save" />
    </p>

    </fieldset>
}

控制器:

    public virtual ActionResult EditProfiles()
    {
        var memberProfileListDto = _memberProfileService.ListByMember(WebSecurity.CurrentUserId);

        var memberProfileListViewModel = Mapper.Map<MemberProfileListDto, MemberProfileListViewModel>(memberProfileListDto);

        return View(memberProfileListViewModel);
    }

如何制作带有 Id 的复选框列表以将更新返回到控制器?

谢谢。

【问题讨论】:

标签: asp.net-mvc razor checkbox


【解决方案1】:

目前foreach 在此上下文中不受支持。尝试使用for 构造,

@for (int i=0; i < blah.count; i++ )
{
    <p>@Html.CheckBoxFor(model => model.blah[i].foo)</p>
}

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2014-11-26
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多