The usage of Razor and HtmlHelper, razorhtmlhelper

Source: Internet
Author: User
Tags what is asp

The usage of Razor and HtmlHelper, razorhtmlhelper
 Use of Razor and HtmlHelper

 

The purpose of this document is to introduce two new concepts in MVC View to beginners, which will help us develop projects faster and better. One is the view engine and the other is HtmlHlper. These two concepts are not new. They are all improved on the ASPX and Html we have learned, making programming easier.

I. Differences between ASPX and Razor

 

In ASP. net mvc mode, there are two types of view engines available: ASPX (ASP. NET) and Razor.

1.ASP.net.

If you select ASPX, the selected engine is actually the ASP. NET engine. Let's talk about these issues before the ASP. NET engine.

What is ASP. NET?

  • ASP. NET is Microsoft. NETMain parts of the framework

  • ASP. NET is a serverScript Technology: Enables (embedded in a webpage) scripts to be executed by Internet servers.

  • It isCombination of ASP and. NET Technologies. Provides component-and event-driven programmable network forms, greatly simplifying programming.

  • ASP. NET is a program running in IIS.

    IIS (Internet Information Services) is a Microsoft Internet Server

    IIS is a free package for Windows servers operating systems.

    IIS is also part of Windows 2000 and XP Professional Edition

What is an ASP. NET file?

  • ASP. NET files are similar to HTML files, but they can contain HTML, XML, and scripts.

  • Execute scripts in ASP. NET files on the server

  • The suffix of the ASP. NET file is ". aspx"

    How does ASP. NET work?

    When the browser requests an HTML file, the server returns the file

    When the browser requests an ASP. NET file, IIS will pass the request to the ASP. NET engine on the server.

    The ASP. NET engine reads the file row by row and executes the script in the file.

    Finally, the ASP. NET file will return to the browser in the form of pure HTML

     

2. Razor
  • What is Razor?

Razor allows you to embed server-based code (Visual Basic and C #) into a webpage.Markup syntax. It can also be seen that Razor is actually a code template mixed with HTML code, similar to a. aspx file without post code.

  • File Type

Razor supports two file types:. cshtml and. vbhtml. The. cshtml server code uses the c # syntax, And the. vbhtml server code uses the vb.net syntax.

  

  • Work:When a webpage is written into a browser, the server-based code can create dynamic content. When a webpage is loaded,Before the server returns a page to the browser, it executes the server-based code on the page.Because it is running on the server, this code can execute complex tasks, such as accessing the database.

3. Comparison

Same:

  • Can be used to insert server code into HTML.

  • Same execution sequence

  • Based on ASP. NET

 

Expertise:

  • Razor is designed for the creation of web applications, especially in MVC3,Service in MVC Mode.

  • It has the capability of marking traditional ASP. NET, but it is more powerful than ASP. NET.Simple syntaxIs easier to use and learn.

Conclusion:

Therefore, for the MVC model project developed this time, we personally suggest using Razor as the development view engine.

Related Syntax of Razor, and usage reference.

Syntax instructions

Reference: http://www.w3cschool.cc/aspnet/razor-intro.html

Ii. HtmlHelper class
  1. Function: Generate Html tags with data binding.

  2. Why? In many cases, when writing a page for editing data, the following code is usually written: <input type = "text" value = '<% = ViewData ["title"] %> 'name = "title"/>. So we thought, can we use a Helper to automatically bind data to this type of code? This is certainly possible. ASP. net mvc provides an HtmlHelper to generate Html tags with data bindings. Look, there is nothing special about this class. It is a tool class that assists us with Html programming. In MVC mode, in View, we can directly use this tool class (HtmlHelper) to read data entity values and assign values to Html tags.

3. Do I have to use HtmlHlper in View?

Mainly Html tags that need to be bound to Data. For example, the form and Models are bound. HtmlHelper is recommended for forms and links in MVC. Other options can be selected in Html and HtmlHelper as needed.


4. Form

 

This is similar to the Html Tag obtained in the form of Html. TextBox.

Use reference: http://www.cnblogs.com/jyan/archive/2012/07/23/2604474.html

Since Microsoft has done so many considerate work for MVC, it is certainly beneficial to MVC and can help us to develop projects faster and better, so we have to work hard to use it.

I hope to help you.

 

Zookeeper
How to Use HtmlHelper (MvcHtmlString) in Razor)

If the HtmlHelper you wrote previously prefers to return a string instead of an MvcHtmlString, You need to modify it after using Razor. Assume that there is a Helper call: <% = Html. ProgressBar (tree, progress) %> and the function declaration is:
Public static string ProgressBar (this HtmlHelper htmlHelper, SFCProgressTree progressTree, SFCProgress progress, bool show = false ){... return ImgTag. toString ()} is changed to @ Html in The New Razor except for the call. progressBar (tree, progress)
In addition, you also need to adjust public static MvcHtmlString ProgressBar (this HtmlHelper htmlHelper, SFCProgressTree progressTree, SFCProgress progress, bool show = false)
} The reason is that all output strings are Html encoded in Razor. In fact, Html. ActionLink and other outputs are MvcHtmlString, but the Helper of the returned string can also be used in aspx. However, only MvcHtmlString can be used in Razor. Click to download the free agile development textbook: Martian agile development manual

Significance of using the html helper method in aspnet mvc

You have to use html and no one will block you.
However, HtmlHelper has many advantages, such as binding forms to Models.
We recommend using HtmlHelper for forms and links.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.