MVC Common features use

Source: Internet
Author: User
Tags try catch

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:

    1. BindAttribute
    2. Remote
    3. HandleError
    4. 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:

  1.  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:

    1. [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; } }
    1. 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:

  1.  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.

    1.  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:

    1. [HandleError ()]  Public actionresult checkerror ()  {     intten;      int 0 ;      int k = A/ b;      return View ();  }

In the Web. config file, we add the following two lines:

    1. <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.

  1. [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.

  1.  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

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.