Guys, I am a little confused with the usage of the keyword model/Model in MVC, so I was hoping somebody could clarify a few aspects of it (needless to say internet searches didn't really help that much).
Given this model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ebuy.Website.Tests.Models
{
    public class Auction
    {
        public long Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public decimal StartPrice { get; set; }
        public decimal CurrentPrice { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
    }
}

And this method in a Auction controller

public ActionResult Details(long id = 0)
        {            
            var auction = new Auction
            {
                Id = id,
                Title = "Brand new Widget 2.0",
                Description = "This is a brand new version 2.0 Widget!",
                StartPrice = 1.00m,
                CurrentPrice = 13.40m,
               // StartTime = DateTime.Parse("6-15-2012 12:34 PM"),
               // EndTime = DateTime.Parse("6-23-2012 12:34 PM"),
               StartTime = DateTime.Parse( "6-15-2012 12:34 PM", CultureInfo.CreateSpecificCulture("en-EN") ),
               EndTime = DateTime.Parse( "6-23-2012 12:34 PM", CultureInfo.CreateSpecificCulture("en-EN") ),
            };
            return View(auction);
        }         

and finally a Create view:

@model Ebuy.Website.Tests.Models.Auction

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
@using (Html.BeginForm()) { 
    <p>
        @Html.LabelFor(model => model.Title)
        @Html.EditorFor(model => model.Title)

    </p>
    ...
}

1)when model is used at the top of a view as in @model Ebuy.Website.Tests.Models.Auction, it is pretty clear, in the sense that it's there because that is a strongly typed view and it allows to use property of the class without fully qualifying them;
2)Not so clear when it is used in the lambda expressions: @Html.LabelFor(model => model.Title). What's model in here? What does it stand for?
3)I have also seen Model used somewhere other than in Ebuy.Website.Tests.Models.Auction, but for the life of me I can't find it now. Any idea what Model stands for?

I mean my understanding is that model is nothing else than the model class, or rather an instance of the class that gets passed over for processing, is that correct?
cheers

Hi

In previous versions of ASP.NET MVC, if you wanted to have a strongly typed view you would have to use syntax similar to:

@inherits System.Web.Mvc.WebViewPage<Type>

but with ASP.NET MVC 3 and above, the @model syntax was introduced so that you could just state:

@model Type

Both do the same thing, but the @model syntax is cleaner.

Not so clear when it is used in the lambda expressions: @Html.LabelFor(model => model.Title). What's model in here? What does it stand for?

This is just an expression and model represents the Auction type. You could just as easily type Html.LabelFor(x => x.Title) as the View is already strongly typed.

I have also seen Model used somewhere other than in Ebuy.Website.Tests.Models.Auction, but for the life of me I can't find it now. Any idea what Model stands for?

Model is a variable and is assigned the type that is used for the view via the @model declaration. So in your example, where you are strongly typing to your Auction type via @model Ebuy.Website.Tests.Models.Auction, Model then contains an instance of this type. This can then be used in your view, for example, <p>@Model.Title</p>.

I mean my understanding is that model is nothing else than the model class, or rather an instance of the class that gets passed over for processing, is that correct?

I guess you could think of it like this, although I think more accurately you would say that model is defining the type that the view expects and Model is assigned to the instance that is passed from the controller.

HTH

Thanks, yes I think it makes a bit more sense now :-). Just one thing:

Model is a variable and is assigned the type that is used for the view via the @model declaration

So is Model (the variable) automatically generated by the system?

So is Model (the variable) automatically generated by the system?

Yes, in fact I believe (but could be wrong) that it is part of the Razor view engine.

OK cool, thanks for that!

As already mentioned in one of the answers the second "model" is just any variable and can be replaced with any other variable name.

I would also suggest to learn more about "lambda expressions" and "extension menthods" while working with ASP.NET MVC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.