Introduction
In previous articles, I discussed with you how to use SINGALR and database notifications to complete a message monitoring application.
In the previous article, I covered how to crud MongoDB in MVC.
Today, I will continue to introduce some of the MVC features that are very useful in development, as follows:
BindAttribute
Remote
HandleError
HiddenInput
Bindattribute
The purpose of using Bindattribute is to restrict users from using appropriate and correct values when submitting form forms. When we submit a form, we check the characteristics that each entity is bound to.
Suppose we already have the following employee entity class:
Public classemployee{ Public stringName {Get;Set; } Public stringEmail {Get;Set; } Public stringAddress {Get;Set; } Public stringPhoneno {Get;Set; }}
To create a employeecontroller, add two action:
[HttpGet] Public actionresult employeeregister () { return View (); } [HttpPost] Public actionresult employeeregister (Employee emp) { return View (); }
To create a view of the first action:
To run this application, fill out the registration form:
If we submit a form, in the second action, we get the following value:
Now if we just want to submit email,name and Phoneno, and we don't want to submit the address, we can add the following feature on the entity class:
[Bind (exclude="Address")] Public classEmployee { Public stringName {Get;Set; } Public stringEmail {Get;Set; } Public stringAddress {Get;Set; } Public stringPhoneno {Get;Set; } }
Bindattribute to use under the SYSTEM.WEB.MVC namespace, using Bindattribute, we can take some control of the field when we submit the form. In the figure below, we have not got the address value in the submitted form data.
We can also use the Bindattribute directly in the action parameter, like this:
Remote Attribute
Suppose we have a registration form, which has a mailbox text box, when entering the mailbox, we want to check whether the entered mailbox is already in the database, if there is, do not submit the form, then we can use Remoteattribute, through Remoteattribute, We can do some service-side validation without submitting the form.
We can use Remoteattribute in the following example:
Public classemployee{ Public stringName {Get;Set; } [Remote ("Checkemail","Employee", errormessage="Email is already exist")] Public stringEmail {Get;Set; } Public stringAddress {Get;Set; } Public stringPhoneno {Get;Set; }}
The first parameter of the Remoteattribute is an action name, the second is the controller name, and the third is the prompt that is displayed to the user if the mailbox already exists. When we finish entering the mailbox, the Checkemail method is executed and the mailbox is checked for existence.
Public Jsonresult checkemail (string Email) { //Check here in thedatabase if it exist in Database return True Else false. return Json (false, jsonrequestbehavior.allowget); }
Here is the effect of execution:
HandleError Attribute
There are many ways to handle exceptions in MVC, such as using try catch, or filter, or through third-party libraries such as Elmah. But MVC also provides a handleerrorattribute to handle exceptions, as follows:
[HandleError ()] Public actionresult checkerror () { intten; int 0 ; int k = A/ b; return View (); }
In the Web. config file, we add the following two lines:
<customerrors mode ="on" defaultredirect ="error.cshtml" ></customErrors>
Create a view error.cshtml under the shared folder and run the program, and if you run the CheckError () method above, the error.cshtml you just created will be displayed.
We can also use Handleerrorattribute to display different view pages for different types of exceptions.
[HandleError (exceptiontype=typeof(DivideByZeroException), view="Dividebyzeroerrorview")][handleerror (Exceptiontype=typeof(NullReferenceException), View ="Nullrefrenceerrorview")] PublicActionResult checkerror () {intA =Ten; intb =0; intK = A/b; returnView (); }
hiddeninput Attribute
If we want to hide some entity fields from our users, we can use the Hiddeninput feature.
Public classEmployee {[Hiddeninput (Displayvalue=false)] Public stringName {Get;Set; } [Remote ("Checkemail","Employee", errormessage="Email is already exist")] Public stringEmail {Get;Set; } Public stringAddress {Get;Set; } Public stringPhoneno {Get;Set; } }
In the above entity, I use the Hiddeninput attribute to describe the name field. After the program runs, the Name field will not be displayed in the browser. So Hiddeninput gives us a little extra control over the entity field.
Summary
my English four exam five times did not pass (heavy participation), is still three level, this article is my first translation, nor the use of translation tools, only by their own understanding, just want to more foreign fresh knowledge and share with you. If the translation is not good also please crossing many include, if the translation of the line, please give me a recommendation , to give the younger brother I continue to translate the power.
Original link: http://www.codeproject.com/Tips/1032266/MVC-Attributes
MVC Common features use