No nonsense MVC Introductory Tutorial notes

Source: Internet
Author: User

Self-study MVC, see the Garden Li Lin write Li Lin write nonsense mvc Introductory Tutorial notes, now some usually ignored or not very clear points down

1,html.dropdownlist

//service-side notation @{        //the value of the drop-down listlist<selectlistitem> SelectList =NewList<selectlistitem>(); Selectlist.add (NewSelectListItem {Value ="1", Text ="list Item One" }); Selectlist.add (NewSelectListItem {Value ="2", Text ="list Item Two" }); } @Html. DropDownList ("ddllist", (SelectList)NewSelectList (Selectlist.asenumerable (),"Value","Text")) //Client Generation<SelectId="ddllist"Name="ddllist"><option value="1"> list item One </option><option value="2"> list Item Two </option></Select>

2, master Page

A, the main content of the page is identified by @renderbody ()

B, @ViewBag. Title is a placeholder for a title that assigns a value to the variable in the control or page.

C, master page @rendersection ("Masterpart", false), @RenderSection method accepts two parameters: ("name", "Whether it is required"), if necessary, then the child page must implement the method, otherwise it will be an error. @section masterpart{Here is the second part of the master page}, @section + space + name {content} to replace the contents of the master page.

D,create as a patial view create custom content,@Html. Partial ("~/views/shared/_usercontrol.cshtml")

E, when layout is not specified on our page, the layout of the page will automatically inherit from _viewstart.cshtml if the item exists globally or in the same folder, if there is a different folder hierarchy _viewstart.cshtml Viewstart.cshtml, the closest one is inherited from the page. _viewstart.cshtml is a special file.

F, if you do not want the page to use any of the master and layout pages, you need to set layout = NULL

3,raddiobutton

Gender: Male @Html. radiobuttonfor (user = user. Sex, 0, new {@name = "sex", @checked = "true"})

 Female @Html. radiobuttonfor (user = user. Sex, 1, new {@name = "sex"})

4, data validation
A
        " Special Numbers " ]        [ Please enter {0}]]        [stringlength {0} between {2} bits to {1} bits " 1 )]        publicstringgetset; }

[Display (name = "Special number")]: View as shown in the name, depending on the effect of the operation.

[Required (errormessage = "Please enter {0}")]:inputnumber is required if the value of the ErrorMessage attribute is not filled in to be displayed on the page. {0} is a placeholder for the Name property in display.

[Stringlength (errormessage = "{0} is between {2} bits to {1} bits", Minimumlength = 1)]: string length: 1-20. {0}, {1}, and {2} are placeholders for parameters, respectively. {0} Name,{1}1{2}20

B,[regularexpression (@ "^[0-5]*$", errormessage = "only 0-5 digits")]: The first parameter is the positive expression to validate, and the second argument is an error message.

C, the inheritance method to achieve common data validation, with a class to inherit Regularexpressionattribute, so as to achieve the encapsulation of the positive expression and error message function, the class code is as follows:

usingSystem;usingSystem.ComponentModel.DataAnnotations;namespaceMVC3. demo.app_code{ Public classSpecialvalidation:regularexpressionattribute { PublicSpecialvalidation ():Base(@"^[0-5]*$") { }         Public Override stringFormaterrormessage (stringname) {            returnString.Format ("{0} is between 0-5", name); }    }}

Use the following code

using System.ComponentModel.DataAnnotations; using MVC3. Demo.app_code; namespace MVC3. demo.models{    Publicclass  validationmodel    {        [specialvalidation]          Public string Get Set ; }    }}

5, rich client with ExtJS and Juqeryui



No nonsense MVC Introductory Tutorial notes

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.