【问题标题】:how do View Models link to the Database?视图模型如何链接到数据库?
【发布时间】:2013-10-28 23:16:45
【问题描述】:

我是 Asp.net MVC 的新手,我真的可以澄清一下 View 模型的工作原理。

据我了解,视图模型仅用于将域模型中的必要字段公开给视图。我发现难以理解的是域模型通过 Dbset 链接到 Db。所以对我来说,当使用域模型将数据发布到控制器时,这些数据可以找到进入 Db 的方式,这对我来说是有意义的。

从我看到的 View 模型的示例中,它们没有被 Dbset 引用。那么发布到视图模型的数据是如何进入数据库的。 EF 是否仅将 View 模型中的字段与域模型中的字段匹配?

感谢您的帮助

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    正如 Jonathan 所说,AutoMapper 将帮助您将 ViewModel 实体映射到您的域模型。这是一个例子:

    在您的视图中,您使用视图模型 (CreateGroupVM):

    @model X.X.Areas.Group.Models.CreateGroupVM
    @using (Html.BeginForm(null,null, FormMethod.Post, new { @class="form-horizontal", role="form"}))
        {
            @Html.ValidationSummary()
            @Html.AntiForgeryToken()
    
            @Html.LabelFor(model => model.Title, new { @class = "col-lg-4 control-label" })
            @Html.TextBoxFor(model => model.Title, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Title)
    
            @Html.LabelFor(model => model.Description, new { @class = "col-lg-4 control-label" })
            @Html.TextBoxFor(model => model.Description, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Description)
    
            @Html.LabelFor(model => model.CategoryId, new { @class = "col-lg-4 control-label" })
            @Html.DropDownListFor(x => x.CategoryId, Model.Categories)
            @Html.ValidationMessageFor(model => model.CategoryId)
    
            <div class="form-group">
                <div class="col-lg-offset-4 col-lg-8">
                    <button type="submit" class="btn-u btn-u-blue">Create</button>
                </div>
            </div>
        }
    

    ViewModel (CreateGroupVM.cs): 请注意我们是如何传递类别列表的——如果您严格使用域模型,则无法这样做,因为您无法在 Group 模型中传递类别列表。这在我们的视图中为我们提供了强类型的帮助器,并且没有使用 ViewBag。

    public class CreateGroupVM
        {
            [Required]
            public string Title { get; set; }
            public string Description { get; set; }
    
            [DisplayName("Category")]
            public int CategoryId { get; set; }
    
            public IEnumerable<SelectListItem> Categories { get; set; }
        }
    

    领域模型(Group.cs):

    public class Group
        {
            public int Id { get; set; }
    
            public string Title { get; set; }
    
            public string Description { get; set; }
    
            public int CategoryId { get; set; }
    
            public int CreatorUserId { get; set; }
    
            public bool Deleted { get; set; }
    
        }
    

    在您的 HttpPost 创建操作中 - 您让 AutoMapper 进行映射,然后保存到数据库。请注意,默认情况下 AutoMapper 将映射同名的字段。您可以阅读https://github.com/AutoMapper/AutoMapper/wiki/Getting-started 以开始使用 AutoMapper。

    [HttpPost]
    [ValidateAntiForgeryToken]
        public ActionResult Create(CreateGroupVM vm)
        {
            if (ModelState.IsValid)
            {
                var group = new InterestGroup();
                Mapper.Map(vm, group); // Let AutoMapper do the work
                db.Groups.Add(group);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(vm);
        }
    

    【讨论】:

    • 感谢您的详细描述。解决了我的困惑。
    【解决方案2】:

    视图模型与您的数据库无关。您需要创建一个新的域模型并使用视图模型中的数据填充它,以便将其保存到数据库中。当然,这样做很烦人,有人创建了AutoMapper 来处理这个问题。

    使用自动映射器,您可以将视图模型中的属性与域模型中的属性相匹配,然后根据需要将它们添加到数据库中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多