Make the View readable in Asp.net MVC.

Source: Internet
Author: User

View in MVC can both write HTML code and C # code. the readability of the Code may become very poor.

In the next project, the backend database uses XML to save the user-submitted custom data. The front-end needs to parse the XML data and output our HTML to present the user a beautiful interface. At the first time, I didn't feel very readable. Because the XML structure was quite easy, I added a lot of For in the View, and the if logic was very short, and the leaders were very satisfied. I did not expect that three months later, the leaders asked for some changes, which would be dumpfounded. What kind of books did I write at that time? The data structure at that time had long been forgotten. You can imagine how much effort you have paid and how many brain cells you have wasted.

For me, the following simple data bonding syntax is acceptable.


<P>
<Label for = "username"> Username: </label>
<% = Html. TextBox ("username") %>
<% = Html. ValidationMessage ("username") %>
</P>
<P>
<Label for = "email"> Email: </label>
<% = Html. TextBox ("email") %>
<% = Html. ValidationMessage ("email") %>
</P>
This requires us to put most of the logic in the Controller, construct the data Model, and then stick it to the View. Because the data model is generally not changed, but the View is often changed.


If necessary, you can write some HTML Helper methods. In a word, try to keep the view readable. What is good readability? In my opinion, each section is responsible for its own affairs. In the future maintenance, you can quickly locate this section, and then quickly modify the gap. It cannot be just a matter of structure.

 

Related Article

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.