Accustomed to the WEB FORM development mode for many years, suddenly switched to MVC, suddenly confused, confused, many are not used to, many do not understand, until now, I have to write down and solve in the application.
\
I’m going to focus on the View.
\
1. @ Html
View has @html.*** all over it, so where did this Html come from? There was no statement.
The Html is actually an HtmlHelper type property of the ViewPage, so it doesn’t need to be declared.
\
2. Also related to HtmlHelper.
Extend the original type HtmlHelper with the following code
namespace System.Web.Mvc.Html
{
public static class HtmlExtensions
{
public static string Span(this HtmlHelper helper, string strId, string strContent)
{
return string.Format("<span id=\"{0}\">{1}</span>", strId, strContent); }}}Copy the code
\
On the view page, call the following \
@html.span ("span1"," This is a Span ")Copy the code
\
See, when you define it with 3 arguments, how can you call it with 2?
It means I’ve been blocked so long, I’m way behind.
In fact, the first parameter has the this modifier, which means that the method is used for objects of the type of the first parameter. For example, in the above code, the Span method is used for an object of type HtmlHelper. \
3, the Model
In a view, Model is an HTML-like thing that can be used without definition. In fact, Model is a property of ViewData. But for some reason, things like ViewPage and ViewData don’t have to be displayed.
\
Lambda expressions
For example, @html.textBoxfor (m => m.name)
A: hi! And where does this M come from? Not that it’s a property of somebody else. TextBoxFor(y => y.name), no problem at all.
Lambda expressions are essentially anonymous functions, such as x => x*x, where x is the argument and x*x is the function body. And the system will guess the type of x based on the body of the function, as in this case, x is probably numeric.
Also, in the view, I guess there’s a view engine or something that will guess from the lambda expression which Model object we want to use and do something about it.
This is not surprising. It’s like making a web page. We write some HTML code, add a little CSS, javascript, whatever, and the result is a colorful web page. Are these lines of code so magical? No, the key lies in the browser, because the browser according to the code to do the corresponding processing and rendering, just have colorful web pages, the core lies in the browser. If opened with Notepad, it would be just a few lines of code.
\
\