Asp.net MVC Model Binding Collection问题

时间:2017-03-20 20:48:49

标签: c# asp.net asp.net-mvc entity-framework model-binding

我在asp.net中遇到问题制作网络服务。当我尝试将集合从视图传递到控制器时,编译器会抛出NullReferenceException(这意味着Model.Modules集合没有传递给控制器​​,因为它是空的 - 我认为下面的视图形式中的foreach循环应该做得很好,但我显然是错的)。有人可以看看我的代码吗?这是我没有进步的第3天,令人沮丧。

P.S。如果我的语法不正确(甚至部分错误),请告诉我,我将不胜感激。

这是我的模特:

public class GroupsModel
{
    public int ID { get; set; }

    [Required]
    [StringLength(30, MinimumLength = 6]
    [RegularExpression("^[a-zA-Z0-9._\\-]{6,}$"]
    public string GroupName { get; set; }

    public virtual ICollection<PermissionsModel> Modules { get; set; }
}

这是两个重要的行动:

[HttpGet]
    public ActionResult Create()
    {
        //This is only for testing
        var data = new GroupsModel();

        data.Modules = new List<PermissionsModel>();

        data.Modules.Add(
            new PermissionsModel(
                "Moduł1", false, false, false, true, true
            )
        );
        data.Modules.Add(
            new PermissionsModel(
                "Moduł2", true, true, true, false, false
            )
        );
        return View(data);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "GroupName, Modules ")] GroupsModel group)
    {
        if (ModelState.IsValid)
        {
            GroupsModel tmp = new GroupsModel();

            tmp.GroupName = group.GroupName;
            tmp.Modules = new List<PermissionsModel>();

            //ill change it to foreach later
            for(int i = 0; i < group.Modules.Count; i++)
            {
                tmp.Modules.Add(group.Modules.ElementAt(i));
            }

            //ill change it to foreach also
            for (int i = 0; i < group.Modules.Count; i++)
            {
                tmp.Modules.ElementAt(i).ModuleName = group.Modules.ElementAt(i).ModuleName;
                tmp.Modules.ElementAt(i).Access = group.Modules.ElementAt(i).Access;
                tmp.Modules.ElementAt(i).Create = group.Modules.ElementAt(i).Create;
                tmp.Modules.ElementAt(i).Edit = group.Modules.ElementAt(i).Edit;
                tmp.Modules.ElementAt(i).GlobalEdit = group.Modules.ElementAt(i).GlobalEdit;
                tmp.Modules.ElementAt(i).Delete = group.Modules.ElementAt(i).Delete;
            }

            //my database, not important here
            db.Groups.Add(tmp);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        return RedirectToAction("Create");
    }

最后查看表格:

@using (Html.BeginForm(FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.LabelFor(m => m.GroupName, "Group name", htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.EditorFor(m => m.GroupName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(m => m.GroupName, "", new { @class = "text-danger" })

    @* JUST FOR TESTING PURPOSE *@
    @{ var i = 0; }
    @foreach (var mdl in Model.Modules)
    {
        @Html.HiddenFor(m => mdl)
        @Html.HiddenFor(m => mdl.ModuleName)
        @Html.CheckBoxFor(m => mdl.Access, new { @class = "form-control" })
        @Html.CheckBoxFor(m => mdl.Create, new { @class = "form-control" })
        @Html.CheckBoxFor(m => mdl.Edit, new { @class = "form-control" })
        @Html.CheckBoxFor(m => mdl.GlobalEdit, new { @class = "form-control" })
        @Html.CheckBoxFor(m => mdl.Delete, new { @class = "form-control" })
        i++;
        <br /><hr />
    }

        <input type="submit" value="Zarejestruj" class="btn btn-default" />
</div>
}

0 个答案:

没有答案