asp.net-mvcvb.netasp.net-mvc-4strongly-typed-view

VB strongly typed model access in MVC4


I am using MVC 4 to create a web application in VB. I have been looking at many online examples for strongly typed views but have found none that actually explain the access of the model. The support and amount of content for MVC in VB in general is horrendous.

Here is my "Project" object which I use to create a strongly typed view:

Public Class Project
    Public Property ProjectID
    Public Property Client
    Public Property Description
    ...
End Class

And here is one of the strongly typed views using the model:

@ModelType MvcApplication1.Project
...
<div class="display-label">
    @Html.DisplayNameFor(Function(model) model.ProjectID)
</div>
<div class="display-field">
    @Html.DisplayFor(Function(model) model.ProjectID)
</div>
...

I only understand how to make calls to the model due to examples online; the function makes no sense to me. When I mouse over DisplayNameFor, Intellisense gives me the following description:

<Extension> Public Function DisplayNameFor(Of Integer)(expression as System.Linq.Expressions.Expression(Of System.Func(Of MvcApplication1.Project, Integer))) As System.Web.Mvc.MvcHtmlString
Gets the display name for the model.

Is this what I am using? If so, it looks nothing like "(Function(model) model.ProjectID)" and why is there a space in between the parameters of the call? I would appreciate any clarification and I apologize in advance for this not being in C#.


Solution

  • Views use a class called a WebViewPage. That's what a view is. The reason you have access to an object called Model is because of this (notice that it's a property of the class). You can access the model at any time in a Razor view with @Model.

    @Html is the way we access Html helpers (HtmlHelper) from our Razor view; useful for generating some HTML for us. DisplayNameFor() is one of many extension methods.

    That long, scary parameter that it wants is actually relatively simple. Basically it wants you to give it an anonymous function. This technique is called a Lambda Expression. In C# (for comparison) it might look like this:

    SomeMethod(x => x.SomeProperty);
    

    It's basically a short-hand way of creating a function on the spot (typically on one line) and doing something with the property. In the case of the Html object, it's methods typically expect a lambda expression involving the model.

    Why do it this way?

    Because it gives you a tremendous amount of power. By using a function you can do anything inside it and anything involving the object (the parameter).

    For example:

    @Html.LabelFor(Function(f) f.Projects.SingleOrDefault(Function(g) g.Id = 5))
    

    What's this do?

    1. I access the view's HtmlHelper object with @Html.
    2. I'm using the method LabelFor to generate a <label for=""> tag.
    3. I begin my lambda expression and give my parameter a name. It can be any name I want and the helper will automatically give me the model (it's designed to do that). In this case I named it f.
    4. Now that I have my object f I need to select which object I'm generating a label for. In this example let's pretend my model isn't a Project but instead it is a different object which contains an array/list of projects.
    5. I select the list of projects (f.Projects) and then I want to select a single project. I do this with SingleOrDefault(). SingleOrDefault takes another lambda and this time it knows to pass in the list of projects as the parameter. I'll call this list of projects g.
    6. Now with the list of projects I will finally select which object.

    The final statement, transferred to English means:

    Given a list of projects from the Model, called "f", take them all and select a single project, called "g", where the Id of the project is equal to 5 and generate an HTML label.