简体   繁体   English

ASP.NET MVC NerdDinner教程问题

[英]ASP.NET MVC NerdDinner Tutorial question

I'm following Scott Guthries MVC tutorial ( http://nerddinnerbook.s3.amazonaws.com/Part6.htm ) and there's something I don't understand. 我正在关注Scott Guthries MVC教程( http://nerddinnerbook.s3.amazonaws.com/Part6.htm ),我有些不了解。

The Controller class called DinnersController has the following Create methods: 名为DinnersController的Controller类具有以下Create方法:

    public ActionResult Create()
    {
        Dinner dinner = new Dinner()
        {
            EventDate = DateTime.Now.AddDays(7)
        };

        return View(new DinnerFormViewModel(dinner));
    }

    [AcceptVerbs( HttpVerbs.Post)]
    public ActionResult Create(Dinner dinner)
    {
        if (ModelState.IsValid)
        {
            try
            {
                dinner.HostedBy = "SomeUser";
                dinnerRepository.Add(dinner);
                dinnerRepository.Save();

                return RedirectToAction("Details", new { id = dinner.DinnerID });
            }
            catch
            {
                foreach (var violation in dinner.GetRuleViolations())
                    ModelState.AddModelError(violation.PropertyName, violation.ErrorMessage);                    
            }
        }

        return View(new DinnerFormViewModel(dinner));
    }

The first method causes the page Create.aspx to be shown which displays form data for the object type 'DinnerViewFormModel' ie 第一种方法导致显示页面Create.aspx,该页面显示对象类型“ DinnerViewFormModel”的表单数据,即

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Controllers.DinnerFormViewModel>" %>

The class 'DinnerViewFormModel' contains a property called 'Dinner' so displaying the relevant information for Dinner type objects is done by calling: “ DinnerViewFormModel”类包含一个名为“ Dinner”的属性,因此可以通过调用以下命令来显示Dinner类型对象的相关信息:

<label for="Title">Title:</label> <%= Html.TextBox("Title", Model.Dinner.Title) %>

I understand whats going on so far. 我了解到目前为止发生了什么。 However, Create.aspx contains a submit type button: 但是,Create.aspx包含一个提交类型按钮:

<input type="submit" value="Create" />

When the button is clicked, the following method is called: 单击该按钮时,将调用以下方法:

    [AcceptVerbs( HttpVerbs.Post)]
    public ActionResult Create(Dinner dinner)

What I don't understand is, If the form's Model data is a 'DinnerViewFormModel' object, how does MVC know what 'Dinner' object needs to be passed to the Create method? 我不明白的是,如果表单的Model数据是“ DinnerViewFormModel”对象,那么MVC如何知道需要将哪些“ Dinner”对象传递给Create方法?

Please could someone enlighten me? 请有人能启发我吗? Thanks 谢谢

It's called "Model Binding" and its built into ASP.NET MVC. 它称为“模型绑定”,它内置于ASP.NET MVC中。 Your action methods need data, and the incoming HTTP request carries the data you need. 您的操作方法需要数据,传入的HTTP请求将携带您需要的数据。 The catch is that the data is embedded into POST-ed form values, and possibly the URL itself. 所要注意的是,数据被嵌入到POST版本的表单值中,并且可能嵌入到URL本身中。 Enter the DefaultModelBinder, which can magically convert form values and route data into objects. 输入DefaultModelBinder,它可以神奇地转换表单值并将数据路由到对象中。 Model binders allow your controller code to remain cleanly separated from the dirtiness of interrogating the request and its associated environment. 模型绑定程序使您的控制器代码与查询请求及其关联环境的肮脏状态保持清晰分离。

AFAIK, MVC just tries to map the properties that are available in the POST to the parameter that it is provided. AFAIK,MVC只是尝试将POST中可用的属性映射到它提供的参数。 Because of that, it does not need to know about the type, it just creates the object with the default constructor and maps the form values to the created object - and that is what you get as the method's parameter value. 因此,它不需要了解类型,只需使用默认构造函数创建对象,然后将表单值映射到所创建的对象-这就是方法参数值。

BTW: You'll also notice that the view output does not contain any reference to the Dinner or DinnerFormViewModel classes. 顺便说一句:您还将注意到,视图输出不包含对DinnerDinnerFormViewModel类的任何引用。

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

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