简体   繁体   English

MVC4自动映射我的视图模型?

[英]MVC4 Automatically mapped my view model?

I began to create my controller for the parent model listed below. 我开始为下面列出的父模型创建我的控制器。 I am using view models for all my views, but before I got around to adding the logic for the Create (Post) action I just hit submit for the hell of it thinking my viewmodel would not map to my model. 我正在为我的所有视图使用视图模型,但在我开始为Create(Post)动作添加逻辑之前,我只是点击提交,因为它认为我的viewmodel不会映射到我的模型。

To my surprise it actually worked. 令我惊讶的是它确实奏效了。 I am using AutoMapper and have set up all the mappings for the child models to their corresponding models, but not the parent model (as absent in the post result). 我正在使用AutoMapper,并已将子模型的所有映射设置为相应的模型,而不是父模型(在后期结果中不存在)。 What is going on here that MVC has allowed such magic to happen? MVC允许这样的魔法发生在这里发生了什么?

Model: 模型:

public partial class ParentModel
    {
        public int Id { get; set; }
        public int Child1Id { get; set; }
        public int Child2Id { get; set; }
        public int Child3Id { get; set; }
        public int Child4Id { get; set; }
        //other data
        public virtual Child1 Child1 { get; set; }
        public virtual Child2 Child2 { get; set; }
        public virtual Child3 Child3 { get; set; }
        public virtual Child4 Child4 { get; set; }
    }

View Model: 查看型号:

public class ParentCreateViewModel
    {
        //other data
        public Child1ViewModel Child1 { get; set; }
        public Child2ViewModel Child2 { get; set; }
        public Child3ViewModel Child3 { get; set; }
        public Child4ViewModel Child4 { get; set; }
    }

View (Create.cshtml): 查看(Create.cshtml):

@model Project.ViewModels.ParentCreateViewModel
@*EditorTemplates and such*@

Controller (Get): 控制器(获取):

public ActionResult Create()
    {
        //some list logic
        return View();
    }

Controller (Post - I haven't yet changed it to the ParentCreateViewModel or AutoMapped it back to ParentModel): 控制器(发布 - 我还没有将它更改为ParentCreateViewModel或AutoMapped它回到ParentModel):

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ParentModel parentModel)
{
    if (ModelState.IsValid)
    {
        db.ParentModels.Add(parentModel);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    //some list logic
    return View(parentModel);
}

My parent model and all child models are persisted correctly to the database. 我的父模型和所有子模型都正确地保存到数据库中。 Does MVC do some kind of behind the scene binding? MVC是否在场景绑定后做某种事情? Shouldn't it be expecting a ParentCreateViewModel? 不应该期待ParentCreateViewModel吗?

The default model binder binds on name of elements. 默认模型绑定器绑定元素名称。 Since both models would share these properties (and therefore, HTML element names when using editor templates and HTML helpers), it will bind it to the model. 由于两个模型都将共享这些属性(因此,在使用编辑器模板和HTML帮助程序时,HTML元素名称),它将绑定到模型。

Both of your models will generate form elements like this: 你的两个模型都会生成这样的表单元素:

<input type="text" name="Child1_Name" />

..etc. ..等等。 When the form values are posted, the model binder inspects and finds "Child1_Name". 发布表单值时,模型绑定器会检查并找到“Child1_Name”。 The underscore signifies a child class property. 下划线表示子类属性。 So, it doesn't matter which model you choose because Child1.Name is a valid property of both models. 因此,选择哪个模型并不重要,因为Child1.Name是两个模型的有效属性。 So, since you've told the action method you want a ParentModel , the model binder happily applies the value to the Child1.Name property when it's found. 因此,由于您已经告诉了您想要ParentModel的action方法,因此模型绑定器会在找到它时将值应用于Child1.Name属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM