⬆️ ⬇️

MVC 2: Strongly Typed Html Helper

asp.net mvc 2 This is the first part of a series of articles about upcoming innovations in ASP.NET MVC 2, the release of which is not far off. This post is dedicated to new strictly typed HTML helpers added to ASP.NET MVC 2.





Existing Html Helpers



ASP.NET MVC came with a set of HTML helpers that can be used in view templates, making it easy to generate the HTML UI. For example, to display a text field, you can write code (in an .aspx view template) that is used by the Html.TextBox () helper method:



image

')

The first parameter of the helper method accepts the name / id of the text field, the second its value. The helper method will generate the HTML, returning it to the browser:



image



New strictly typed HTML helpers



One of the most frequently requested features of the second version was support for strongly typed HTML helpers that use lamba expressions when referring to models or view models that are passed to the view template. That allows you to check the type at the time of compilation, therefore errors can not be detected while the application is running. It also improves intellisense support in the view template.



New, strongly typed HTML helpers are now an integral part of ASP.NET MVC 2. These methods use the “Html.HelperNameFor ()” naming rules. for example: Html.TextBoxFor (), Html.CheckBoxFor (), Html.TextAreaFor (), etc. They support lambda expressions to determine the name / id and element value.



For example, we can use the new Html.TextBoxFor () helper in addition to the Html.TextBox ():



image



Note that we no longer need to define the “ProductName” string parameter — lambda expressions are flexible enough so that we can return both property names and fields in our model object in addition to its value.



In consequence of the strict typing of the HTML helper, we get full support for intellisense in Visual Studio when we write lambda expressions.



image



The generated HTML is exactly the same as the previous version shown above.



image



List of strongly typed HTML helpers built into ASP.NET MVC 2



HTML helpers for items:

Other helpers:I’ll tell you about the new Html.EditorFor () and Html.DisplayFor () helpers in the next section, when I’ll touch on the topic of auto-scaffold functionality in ASP.NET MVC 2. We’ll also use the Html.ValidationMessageFor () helper when we look at the validation topic.



Strongly typed HTML helpers and scaffolding



Now VS 2008 and VS 2010 by default use new strongly typed HTML helpers when they create new strongly typed view templates using the Add View command.



For example, let's imagine that we have a simple class “ProductsController”, whose action method is “Edit”, which in turn generates an editing form for the model “Product”:



image



We can right-click on the Edit action method in Visual Studio and select “Add View” to create a view template. We will choose to create an “Edit” template, through scaffolding, using the Product object:



image



With ASP.NET MVC 2, a default view template that is created using new strongly typed HTML helpers to reference the Product model object:



image



findings



New strongly typed HTML helpers in ASP.NET MVC 2 provide the opportunity to get improvements: type safety in your view templates, as well as view checking at compile time and intellisense support while editing your templates in Visual Studio.

Source: https://habr.com/ru/post/80546/



All Articles