I am following the Scott Guthries MVC study guide ( http://nerddinnerbook.s3.amazonaws.com/Part6.htm ), and there is something I don’t understand.
The Controller class called DinnersController has the following Create methods:
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 calls the display of the Create.aspx page, which displays the form data for the object type "DinnerViewFormModel" ie
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Controllers.DinnerFormViewModel>" %>
The "DinnerViewFormModel" class contains the "Dinner" property, therefore, displaying the corresponding information for objects of the "Dinner" type is done by calling:
<label for="Title">Title:</label> <%= Html.TextBox("Title", Model.Dinner.Title) %>
I understand what is happening so far. However, Create.aspx does contain a submit type button:
<input type="submit" value="Create" />
When a button is pressed, the following method is called:
[AcceptVerbs( HttpVerbs.Post)] public ActionResult Create(Dinner dinner)
I don’t understand if the model data form is a DinnerViewFormModel object, how does MVC know that the “Dinner” object needs to be passed to the Create method?
Please can someone enlighten me? Thanks