MVC Notes (ii)

Source: Internet
Author: User

Httputility.htmlencode to preprocess user input, which prevents users from injecting JS code or HTML markup into the view with a link
1. [Required]: Non-null verification
2, [Stringlength (* *)]: Set the length of the character
2.1, [Stringlength (**,minimumlength=*): Set the character length minimumlength: Set the shortest length
3, [Regularexperssion (@ "Regular expression")]: Regular validation
4. The [Range (,)]:range attribute is used to specify the minimum and maximum values for a numeric type value, the range attribute can be used with an int type and a double type, and it also has an overloaded Range (Typeof (decimal), "0.0", "49.99"]
Two. validation features;
2.1[remote ("Checkusername", "Account")]: Specifies the action method to invoke to perform this validation.
and the controller where this action is located. The front is the controller behind the action
2.2[compare ("Authentication type")]: used to ensure that two properties of the model object have the same value


2.3 To validate the form, put the submit on the table order
Three: Viewdata,tempdata,viewbag
Both ViewData and TempData can pass weakly typed data, with the following differences:

The ViewData is valid only in the current Action, with the same life cycle and View;

TempData data can only be passed once by the controller, and each element can only be accessed once, after access, automatically deleted.

TempData is typically used for temporary caching of content or for passing error messages when an error page is thrown, TempData can be stored in the appropriate ViewData before use for recycling.
TempData can pass values across pages

ViewBag and ViewData have the same life cycle, but the former view is valid, except that the type of viewbag is no longer the key-value pair structure of the dictionary, but dynamic type, which belongs to the new part of MVC3.

Four:
The public method in the controller is treated as an action method.

Five: @ajax. ActionResult
Use @ajax. ActionResult should refer to <script src= "~/scripts/jquery.unobtrusive-ajax.js" ></script> files first

VI: @Html. Partial ("_logonpartial"): equivalent to a user control, referring to a user control named "_logonpartial"

A.layout property: Equivalent to the original MasterPageFile property.

[Email protected] () method: Renders the entire view directly to the placeholder, without needing the original <asp:content/>.

[Email protected] () Method: Renders the specified page at the placeholder.

[Email protected] Method: Declares a placeholder, similar to the original <asp:contentplaceholder/> function.

[email protected] Tag: The placeholder for the @rendersection method declaration is implemented, similar to the original <asp:content/> function.

URL: "Controll"/"Action"
Renderaction: "Action"/"Controll"


Both the html.renderpartial and Html.renderaction methods are used to embed user controls on the interface.

@Html. RenderPartial ("User Control Name")//must be placed in the same directory as the caller, or it can be placed in view/shared
@Html. Renderaction ("Action", "Control");//


@Html. ActionLink ("name", "Action", "Controll", "parameter", "Property name")


Six: Routing
Mention here the string[] namespaces parameter. ~
When you build a controller class namespace in your MVC3 application that does not end in controllers, you can set this property to make way for the system to find those namespaces when the URL matches
Routes. MapRoute ("Namespaces",
"{controller}/{action}",
NULL, NULL,
New string[] {"Mvcapplication1.custom"}
);

After this parameter is set, the system will look for it.
Mvcapplication1.controllers, Mvcapplication1.custom with controller at the end of the class is inherited!

Examples of ⑦:url overrides
"{controller}." {Action}/{id} ",//Url//url format with parameters is Http://localhost:2741/home.Index

The parameters in the URL rewrite must be consistent with the parameters defined in the route, such as to pass the {productname}-{id}//page when used to New{productname= "xx", id= "XX"}

FormCollection: Can get the value of table only son (not recommended) refer to "Traps using formcollection".
Cases
Public ActionResult Index (FormCollection collection)
{
String name = collection["Name"];

return View ();
}

Seven, Codefrist
The name of the configuration file must be the same as the name of the class that inherits the DbContext, and the program will run to automatically find the connection string for the configuration file (reflection ...). Requirements for namespaces are strict)

Asnotracking: Returns an entity returned by a new query that will not be cached in DbContext or ObjectContext.

Database.setinitializer<dbentities> (null);//Do not re-set the database

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.