Razor: Common mistakes and proper writing methods from aspx to cshtml

Source: Internet
Author: User

http://blog.csdn.net/cheny_com/article/details/6298496

There are a lot of places to adapt from ASPX to chshtml, and this post is an update to the individual learning notes. It is programmed with this thread every day.

Sort by the first letter of a meaningful compilation error to make it easier to find:

Cannot implicitly convert type ' void ' to ' object '

Error: @Html. renderpartial ("_xxxx", Model);

Correct: @{html.renderpartial ("_xxxx", Model);}

Other: This writing profoundly shows that the code "<% xxx;%>" becomes @{xxx;

But it feels so ugly, is there a better one?

' object ': type used in a using statement must is implicitly convertible to ' system.idisposable '

Error: @using "...";

Correct: @using ...; (remove the quotation marks)

Explanation: It is possible to understand that, in addition to a more than a @, and the CS file in the same syntax.

The name ' I ' does not exist in the current context

Error:
@{
    <table>
            for (int i = 0; I <=; i++)
            {
                <tr>
                       //here, I
                 </tr>
           }
   </ Table>
}
is correct:

<table>
@for (int i = 0; I <=; i++)
{
<tr>
I was used here.
</tr>
}
</table>

Any <> will change from C # syntax to HTML syntax, while @{} is the opposite.

-----------------------------------------------------------------------------------------------

Not good: (can also run)

        <td>
             @foreach (var user in Roles.getusersinrole ((string) viewbag.oldrole))
             {
                 <text> @user <br/></text>
            
        </td>
Good:

<td>
@foreach (var user in Roles.getusersinrole ((string) viewbag.oldrole))
{
@user <br/>
}
</td>
Note: @ In addition to the context from HTML to CS, can also do the opposite change. The simplicity of this code is much better.

Description: I thought razor would like to "Mosaic C # in HTML" into "embedded HTML in C #" (Like Helper). The advantage of the latter is that it can be tested while the former is not. In the launch of Razor when the official website once mentioned to let Razor can test, do not know how to achieve, wait and see.

Click to download the free Agile Development textbook: The Martian Agile Development Handbook

Razor: Common mistakes and proper writing methods from aspx to cshtml

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.