ASP. MVC4 + Oracle + Easyui + Bootstrap Chapter I-Operational data and validation
This article link: http://www.cnblogs.com/likeli/p/4234238.html
1, about HtmlHelper and Urlhelper
Because the goal of most Web requests is to send HTML code to the client. Therefore, ASP. NET MVC also provides various help for us to create HTML. In addition to razor, two of the most important helper classes are HtmlHelper and urlhelper, which are exposed as HTML and URL attributes for the controller and view for our use.
An example of a helper class is listed here:
1 < src= "@Url. Content ("/>2 @Html. ActionLink ("homepage", " Index "," Home ")
This part of the code is rendered after:
1 < src= "/admin/content/images/1.jpg"/>2< href= "/admin/home/index">homepage</a>
These two helper classes simply extend some of the additional behavior of the framework. There are too many ways to extend them, but it is not possible to enumerate them here.
Emphasize: HtmlHelper help us generate HTML code, Urlhelper help us generate URL address, so just remember, when we need to generate HTML code or URL, we should use them.
2, simple landing combat
First, sketch a landing style,
Figure 1
Build a simple model, controller, and generate a view.
Figure 2
When creating a view, create a blank view without the EF build.
1 Publicactionresult Login ()2 {3 returnView ();4 }5 6 [HttpPost]7 PublicActionResult Login (Loginmodel model)8 {9 if(modelstate.isvalid)Ten { One //Simple validation through A returnRedirectolocal ("~/admin/home"); - } - Else the { - //if something goes wrong when we go to this step, re-display the form -Modelstate.addmodelerror ("","the user name or password provided is incorrect"); - returnView (model); + } -}
The above code is the code of the login controller. and made a simple verification. After validation passes, jump to the specified page.
When it comes to data problems, there are a number of rules and restrictions that can be used, such as fields that are not empty, setting range ranges for fields, and so on. In MVC, the controller can check whether the request is valid by manipulating modelstate.
1 Public classLoginmodel2 {3 [Required]4[Display (Name ="User name")]5[Stringlength (Ten, errormessage ="must contain a minimum of {2} characters", Minimumlength =3)]6 Public stringUserName {Get;Set; }7 8 [Required]9[Display (Name ="Password")]Ten Public stringuserpwd {Get;Set; } One A [Required] -[Display (Name ="Remember me? ")] - Public BOOLRememberMe {Get;Set; } the}
For the above required, Display, and stringlength tags, this is to enhance the control of this property.
- Focus on custom error messages. The data token provides a ErrorMessage property that specifies the error message to be returned to the user. Instead of the default information generated by the data Annotations API. Just like in the code above that defines the character length.
Once you have defined the error message, now is how you want to display the error message. viewdata.modelstate["UserName"] can read this error message, but this is a collection, we need to use an iterative loop to display the error message, it is inevitable to write a loop on the page to output error messages. However, MVC now provides a better way to render error messages for specific properties @Html. Validationmessagefor (String modelname), this method eliminates the loop code. as follows, a code can complete the effect:@Html. validationmessagefor (M = m.username)
- Here again, the authentication method used here is in the service side of the code validation, need to go back and forth between the server and the client to verify the validity of the data, then smart you should immediately understand what I want to say, that is, this verification method is not a good way to conserve bandwidth and server resources. However, it is undeniable that this is an effective way to ensure the validity and security of the data. Selective use, and later in the article we will optimize the verification method to avoid unnecessary requests, save bandwidth and server resources.
View, we need to use HtmlHelper and urlhelper to generate URLs and HTML tags.
1 @using (Html.BeginForm ())2 {3 <Divclass= "Loginbox">4 <ul>5 <Li>6 @Html. textboxfor (M = m.username, new {@class = "Loginuser", @placeholder = "Please enter user name"}) 7 @Html. validationmessagefor (M = m.username)8 </Li>9 <Li>Ten @Html. textboxfor (M = m.userpwd, new {@class = "loginpwd", @placeholder = "Please enter password"}) One @Html. validationmessagefor (M = m.userpwd) A </Li> - <Li> - <inputtype= "Submit"class= "Loginbtn"name= "Slogin"value= "Login" /> the <label> - @Html. checkboxfor (M = m.rememberme, new {@checked = "checked"}) remember the password - </label> - <label> + <ahref= "javascript:;">Forgot your password?</a> - </label> + </Li> A </ul> at </Div> -}3. Reference documents
"ASP. NET MVC4 Web Programming", MSDN
4. Related Downloads
Source code: http://download.csdn.net/detail/a406502972/8376701
5. PostScript
This article is a lengthy process, because it is self-learning, so I will be fine to every problem I encounter, every detail, so writing a blog is slower.
Also particularly hope, you bo friends in the process of reading, a lot of points, on the one hand refinement of my learning to understand, but also strengthen my ability to write documents.
I sincerely hope that I can evolve from a side dish to a calf and then to Daniel. Hope to read the Bo friends can also be from my learning process to achieve progress!
ASP. MVC4 + Oracle + Easyui + Bootstrap First Chapter