Front-end rendering @Helper directives in MVC 4

Source: Internet
Author: User

If we need to display in a page or multiple pages, such as the format of the renminbi (no ¥ back in the background) ¥ formatting, or the background data for example, such as keeping a number of small number of processing, these things are often used, especially some of the null value of processing, there may be errors. We can then encapsulate a method by creating a. cshtml file (and of course you can write the method in the current page that you want to use).

It can be called directly in the front-end of each used.

First call on this page, and write on this page using the following:

@{

Layout= "~/views/shared/_layout.cshtml";

}

@using Mycon.user

@helper Show ()

{

<p style= "color:red;" > @Model .name</p>

}

Call

@ Show ()//Note no;

If multiple views or templates are to be called, then we can define a. chhtml file in the App_Code directory, which is usually at the root of the project, so that all views or templates within the project can be called.

Below:

Add a common.cshtml file inside the App_Code folder and write the method with @helper.

@helper displayprice (Decimal pri)

{

if (pri==0)

{

<span> Price Undecided </span>

}

Else

{

<span> @string. Format ("{0:c2}", pic) </span>

}

}

@helper showmessage (int value=0)

{

@: I am @value

}

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.