在MVC asp.net中保存多对多

时间:2016-08-22 09:20:08

标签: asp.net-mvc ado.net entity-framework-6

我读了很多关于MVC的内容。但是直到现在还没有明确的答案如何处理多对多的关系,因为这些教程中的许多结果都有同样的错误 我正在使用数据库第一种方法,我的应用程序应该添加用户,同时添加用户我应该有角色列表,我可以将这些角色添加到此用户或相反我可以拥有用户列表和编辑的角色,我们知道当我将数据库与破坏表添加到ado.net时,关系将是多对多 我已成功添加角色列表,但是当我按下保存时,我得到了空引用的例外 这是我的代码:

首先是自定义视图:

namespace UserMangment.ViewModel
{
    public class RolesViewMode
    {
        public tab_OnlineUsers OnlineUsers { get; set; }
        public IEnumerable<SelectListItem> AllRolesnames { get; set; }

        private List<string> _selectedRolesname;


        public List<string> SelectedRoleTags
        {
            get
            {
                if (_selectedRolesname == null)
                {
                    _selectedRolesname = OnlineUsers.tab_OnlineRoles.Select(m => m.Id).ToList();
                }
                return _selectedRolesname;
            }
            set { _selectedRolesname = value; }
        }


    }
}

然后是控制器:

namespace ELVIRA_UserMangment.Controllers
{
    public class OnlineUsersController : Controller
    {
        private Entities db = new Entities();

        // GET: OnlineUsers
        public ActionResult Index()
        {
            return View(db.tab_OnlineUsers.ToList());
        }

        // GET: OnlineUsers/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tab_OnlineUsers tab_OnlineUsers = db.tab_OnlineUsers.Find(id);
            if (tab_OnlineUsers == null)
            {
                return HttpNotFound();
            }
            return View(tab_OnlineUsers);
        }

        // GET: OnlineUsers/Create
        public ActionResult Create()
        {
            var RolesViewmode = new RolesViewMode
            {
                OnlineUsers = db.tab_OnlineUsers.Create(),
            };
            if (RolesViewmode.OnlineUsers == null)
                return HttpNotFound();

            var allrolles = db.tab_OnlineRoles.ToList();
            RolesViewmode.AllRolesnames = allrolles.Select(o => new SelectListItem
            {
                Text = o.Name,
                Value = o.Id.ToString()
            });
            return View(RolesViewmode);
        }

        // POST: OnlineUsers/Create
        // Aktivieren Sie zum Schutz vor übermäßigem Senden von Angriffen die spezifischen Eigenschaften, mit denen eine Bindung erfolgen soll. Weitere Informationen 
        // finden Sie unter http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] RolesViewMode tab_OnlineUsers)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.tab_OnlineUsers.Add(tab_OnlineUsers.OnlineUsers);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch(DbEntityValidationException e)
                {
                    Console.WriteLine(e);
                }

            }

            return View(tab_OnlineUsers);
        }

        // GET: OnlineUsers/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var RolesViewmode = new RolesViewMode
            {
                OnlineUsers = db.tab_OnlineUsers.Include(i => i.tab_OnlineRoles).First(i => i.Id == id),
            };
            if (RolesViewmode.OnlineUsers == null)
                return HttpNotFound();

            var allrolles = db.tab_OnlineRoles.ToList();
            RolesViewmode.AllRolesnames = allrolles.Select(o => new SelectListItem
            {
                Text = o.Name,
                Value = o.Id.ToString()
            });

            tab_OnlineUsers tab_OnlineUsers = db.tab_OnlineUsers.Find(id);
            if (tab_OnlineUsers == null)
            {
                return HttpNotFound();
            }
            return View(RolesViewmode);
        }

        // POST: OnlineUsers/Edit/5
        // Aktivieren Sie zum Schutz vor übermäßigem Senden von Angriffen die spezifischen Eigenschaften, mit denen eine Bindung erfolgen soll. Weitere Informationen 
        // finden Sie unter http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] tab_OnlineUsers tab_OnlineUsers)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tab_OnlineUsers).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tab_OnlineUsers);
        }

然后最后创建视图:

@model UserMangment.ViewModel.RolesViewMode

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>tab_OnlineUsers</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.OnlineUsers.Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model =>model.OnlineUsers.Id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model =>model.OnlineUsers.Id, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

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

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

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

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

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

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

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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.AllRolesnames, "Roles Name", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.ListBoxFor(m => m.SelectedRoleTags, Model.AllRolesnames)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>


}

所以这段代码成功带来了列表,但是将这些信息保存到我的数据库中会出错。

1 个答案:

答案 0 :(得分:1)

让我向您探讨一种可以解决多对多关系问题的技巧。 当您在数据库中遇到多对多关系时,您应该为数据库中的两个表创建一个查找表,如:

如果您有角色和用户的表(如果有多个角色分配给用户),那么您应该为此工作创建一个查找表,其中保存RoleId和UserId(Id,RoleId,UserId等等)任何字段你想添加)..

每当角色分配给用户时,你应该在该表中输入一个条目。

可能有更多可能的解决方案,但这是我在面对多对多关系问题时所做的事情。

谢谢,

希望这会有所帮助......