12. ivalueprovider for new features

Source: Internet
Author: User

In the newly released ASP. net mvc beta version, an overload with the ivalueprovider parameter is provided in the updatamodel method. So what is the use of this ivalueprovider?

Let's take a look at a simple scenario. For example, our blog system has a post object, which has a tags attribute and categories attribute. Their types are:

Post. Tags: statelist <string> (an extension type of list <t> In blogengine. Net)
Post. Categories: statelist <Category>

 

If we want to use the updatamodel method to update the form data we post to our post object, we may have the following code:

/// <Summary>
/// Save the submitted content of the new essay form to the database
/// </Summary>
[Acceptverbs ("Post"), actionname ("newpost")]
Public actionresult savenewpost (formcollection form)
{
Post post = New post ();
Try
{
Updatemodel (post, new [] {"title", "content", "slug", "tags", "categories "});
}
Catch
{
Return view (post );
}
..
}

Obviously, in the above Code, when we use updatemodel to update the tags and categories attributes, it is impossible to succeed, the updatemodel method does not know how to convert the "tags" and "categories" data submitted by form to the statelist <string> type and statelist <Category> type. At this time, we need to provide a valueprovider for this conversion.

To implement a valueprovider, we only need to implement the getvalue method of the ivalueprovider interface and return the result of a valueproviderresult. Next we will write a postvalueprovider to implement the above situation. The Code is as follows:

Postvalueprovider
Public class postvalueprovider: ivalueprovider
{
Private controllercontext context;
// Private defavaluvalueprovider dprovider;

Public postvalueprovider (controllercontext context)
{
This. Context = context;
// Dprovider = new defaultvalueprovider (context );
}

# Region ivalueprovider Member

Public valueproviderresult getvalue (string name)
{
If (string. isnullorempty (name ))
{
Throw new argumentexception ("parameter cannot be blank", "name ");
}
Switch (name)
{
Case "tags ":
Return gettagsvalue ();
Case "categories ":
Return getcategoriesvalue ();
Default:
Return new defaultvalueprovider (context). getvalue (name );
}
}

# Endregion

Private valueproviderresult gettagsvalue ()
{
String strtags = getvaluefromrequest ("tags ");
If (string. isnullorempty (strtags ))
{
Return NULL;
}

String [] tags = strtags. Split (New String [] {","}, stringsplitoptions. removeemptyentries );
Statelist <string> tagslist = new statelist <string> ();
Foreach (string tag in tags)
{
Tagslist. Add (tag. Trim (). tolowerinvariant ());
}

Return new valueproviderresult (tagslist, strtags, cultureinfo. invariantculture );
}

Private valueproviderresult getcategoriesvalue ()
{
String strcategories = getvaluefromrequest ("categories ");
If (string. isnullorempty (strcategories ))
{
Return NULL;
}

String [] categories = strcategories. Split (New String [] {","}, stringsplitoptions. removeemptyentries );
Statelist <Category> List = new statelist <Category> ();
Foreach (string C in categories)
{
List. Add (category. getcategory (New GUID (c )));
}

Return new valueproviderresult (list, strcategories, cultureinfo. invariantculture );
}

Private string getvaluefromrequest (string name)
{
String value = NULL;
Httprequestbase request = context. httpcontext. request;
If (request! = NULL)
{
If (request. querystring! = NULL)
{
Value = request. querystring [name];
}
If (string. isnullorempty (value) & (request. form! = NULL ))
{
Value = request. Form [name];
}
}

Return value;
}
}

Then we can use our postvalueprovider in the updatemodel method:

 

/// <Summary>
/// Save the submitted content of the new essay form to the database
/// </Summary>
[Acceptverbs ("Post"), actionname ("newpost")]
Public actionresult savenewpost (formcollection form)
{
Post post = New post ();
Try
{
Updatemodel (post, new [] {"title", "content", "slug", "tags", "categories"}, new postvalueprovider (controllercontext ));
}
Catch
{
Return view (post );
}

..
}

Enjoy! Post by Q. Lee. Lulu.

For more information about ASP. net mvc, refer to the ASP. net mvc getting started series. The sample code in this article can also be downloaded from the ASP. net mvc getting started series.

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.