MVC Little Memo

Source: Internet
Author: User

1, page with <dl><dt><dd> and Bootstrap "Dl-horizontal" class layout page, so that each property name and attribute value one line

<DLclass= "Dl-horizontal">        <DT>@Html. displaynamefor (model = model. FirstName)</DT>        <DD>@Html. displayfor (model = model. FirstName)</DD></DL>

2, function html.enumdropdownlistfor, must be MVC5 to use

@Html. enumdropdownlistfor (model = model. SEX)

It generates a drop-down menu based on the enum variable of sex. This enum is defined as follows:

public enum sex{     Male, Female}

Then it generates the following drop-down menu code:

<select data-val= "true" data-val-required= "Sex field is required. "Id=" Sex "name=" sex "><option selected=" selected "value=" 0 ">male</option><option value=" 1 "> Female</option></select>

3. The function of the @Html. AntiForgeryToken () function is to protect against cross-site request forgery of Web pages (CSRF cross-site requests forgery). This vulnerability could be exploited as a login user to send a malicious request. For example, if a user logs on to an online bank and then visits the attacker's website, the website will make a request for the bank information via the login user.

The parameters of the 4,action are passed as a worker instance. That is, the 4 values submitted by create.cshtml are assigned to work and the worker is passed to create as a parameter. The [Bind (Include = "FirstName, LastName, Sex, Rating")] in front of this parameter is designed to prevent excessive commit (overposting) attacks. From the create.cshtml code can be known, this page will only submit 4 values. And hackers can have a way to submit more values through this page to the current action, and these extra values will also exist in the worker instance is added to the database, which is undoubtedly dangerous. So [Bind (Include = "]] limits the number of values you submit, and I accept only the 4 values of" FirstName, LastName, Sex, Rating "in this action. Ensure the security of the page.

The 5,modelstate.addmodelerror () function can add an error message to the model, the first parameter of the function is key, which is used to find the error message, and the second parameter is the specific content of the error message. This error message can be reached via Html.validationmessage ("Unabletosave") in view.

MVC Little Memo

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.