微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET MVC 5 多选在 POST 期间未绑定

如何解决ASP.NET MVC 5 多选在 POST 期间未绑定

我是 ASP.NET MVC 5 的新手,在提交表单时在 POST 期间仅绑定正文的多选部分时遇到了一些问题。表单正确呈现,复选框被正确选中。表单使用visual studio搭建脚手架(多选除外)

    public class Editviewmodel
    {
        [required]
        [display(Name = "First Name")]
        public string FirstName { get; set; }

        [required]
        [display(Name = "LastName")]
        public string LastName { get; set; }
        public IList<Rolesviewmodel> Roles { get; set; }

    }


    public class Rolesviewmodel
    {
        public string RoleId { get; set; }
        public string RoleName { get; set; }
        public bool Selected { get; set; }
    }


      [HttpGet]
        public async Task<ActionResult> Edit(string Id)
        {...
            var model = Mapper.Map<ApplicationUser,Editviewmodel>(user); 
            return View(model);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit(Editviewmodel model)
        {
        }

@model BloggingService.Web.Models.Editviewmodel

<!DOCTYPE html>

<html>
<head>
    <Meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        
    <div class="form-horizontal">
        <h4>Edit</h4>
        <hr />
        @Html.ValidationSummary(true,"",new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.Email,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email,new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FirstName,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FirstName,new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LastName,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LastName,new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName,new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.Roles,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">


                @for (int i = 0; i < Model.Roles.Count; i++)
                {

                    @Html.CheckBoxFor(x => @Model.Roles[i].Selected,"test1");
                    <div class="form-check">
                        <input type="hidden" asp-for="@Model.Roles[i].RoleId" />
                        <input type="hidden" asp-for="@Model.Roles[i].RoleName" />
                        <input type="checkBox" asp-for="@Model.Roles[i].Selected" class="form-check-input" checked="@Model.Roles[i].Selected" />
                        <label class="form-check-label" asp-for="@Model.Roles[i].Selected">
                            @Model.Roles[i].RoleName
                        </label>
                    </div>
                }
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
    }
    
    <div>
        @Html.ActionLink("Back to List","Index")
    </div>

非常感谢任何见解。

解决方法

通过利用我的 for 循环中的 HTML 辅助方法,我设法找到了完成这些任务的正确和更清晰的方法,现在数据能够在数据绑定期间正确绑定。

  @Html.HiddenFor(x => @Model.Roles[i].RoleId);
  @Html.HiddenFor(x => @Model.Roles[i].RoleName);
  @Html.CheckBoxFor(x => @Model.Roles[i].Selected);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。