ASP. NET MVC: Application Bindattribute

Source: Internet
Author: User
Tags class definition

Environment:

Windows V, VS. SP1, ASP. NET MVC RC1

------------------------------------------------------------------------------

In this article, we describe how to implement a custom modelbinder, and continue to explore how to use Bindattribute to customize the properties of the model binding.

Once again, we mentioned the book class, Bookcontroller.create Action, and the view of Create in a previous article.

Book Class:

Create Action:

The corresponding view:

Please note that we do not apply the custom Modelbinder described in the previous article. Now, when we submit the form, in the action of create, we can get a book instance, the BookName of this instance, the Author, the publisheddate are all expected to get the corresponding form value.

OK, now, for some reason, it could be a security consideration, and so on, I want to only customize the value of the binding from the form in action, which means I want to filter the property of the model custom binding.

In ASP. Bindattribute, this role is played.

There are three important members in Bindattribute: String Exclude, String Include, String Prefix. "Exclude" is used to specify the property to exclude, "include" is used to specify the property to be included, "Prefix" is used to specify the prefix.

First say, "Prefix" bar, by default, Defaultmodelbinder can automatically recognize the name of the action parameter name prefix, that is, in the view above, give each textbox a "book" prefix, do not need to make any changes, You can still get an instance of book in action.
Add a view with the prefix "book":

At this point, if we add the prefix is not "book", then we need bindattribute assistance, assuming that we add a "B" prefix:

So, in order to get the corresponding book instance in action, you need to apply Bindattribute on the book parameter of the action:

Now look at "Exclude" and "Include", actually these two things can be applied one at a time. Now I want the binding to exclude both "bookname" and "Author" (of course, it doesn't make sense here). For the sake of simplifying the problem, view removes the prefix of the textbox name: Then, change the action to:

By default, multiple property is separated by commas.
Bindattribute out the arguments that can be applied to the action, and can also be applied to the model class definition:

If Bindattribute is applied in the model class definition and on the action parameter, then the intersection of the two is taken. Personally, it is better to try to avoid them fighting.

Well, Bindattribute on this, originally to combine bindattribute continue to improve the previous article Simplemodelbinder, or leave the next article it.

Ext.: http://www.cnblogs.com/tristanguo/archive/2009/02/28/1400186.html

ASP. NET MVC: Application Bindattribute

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.