tags:

views:

104

answers:

2

Hi,

I'm following Scott Guthries MVC tutorial (http://nerddinnerbook.s3.amazonaws.com/Part6.htm) and there's 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 causes the page Create.aspx to be shown which displays form data for the object type 'DinnerViewFormModel' i.e.

<%@ 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:

<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:

<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?

Please could someone enlighten me? Thanks

+3  A: 

AFAIK, MVC just tries to map the properties that are available in the POST to the parameter that it is provided. 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.

hangy
Ok, that makes sense. Knew there had to be a straightforward answer.Thanks for your help
xiang
+5  A: 

It's called "Model Binding" and its built into ASP.NET MVC. Your action methods need data, and the incoming HTTP request carries the data you need. The catch is that the data is embedded into POST-ed form values, and possibly the URL itself. Enter the DefaultModelBinder, which can magically convert form values and route data into objects. Model binders allow your controller code to remain cleanly separated from the dirtiness of interrogating the request and its associated environment.

Climber104