In ASP. net mvc Beta, 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>(A List in BlogEngine. NET<T>Extension type)
- Post. Categories: StateList<Category>
If we want to use the UpdataModel method in ASP. net mvc to update the Form data we Post to our Post object, there may be 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)
- {
- PostPost=NewPost ();
- 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
- PublicclassPostValueProvider: IValueProvider
- {
- PrivateControllerContextcontext;
- // Privatedefavaluvalueproviderdprovider;
-
- PublicPostValueProvider (ControllerContextcontext)
- {
- This. context= Context;
- //DProvider=Newdefavaluvalueprovider(Context );
- }
-
- # RegionIValueProvider Member
-
- PublicValueProviderResultGetValue (stringname)
- {
- If (string. IsNullOrEmpty (name ))
- {
- ThrownewArgumentException ("parameter cannot be blank", "name ");
- }
- Switch (name)
- {
- Case "Tags ":
- ReturnGetTagsValue ();
- Case "Categories ":
- ReturnGetCategoriesValue ();
- Default:
- Returnnewdefavaluvalueprovider (context). GetValue (name );
- }
- }
-
- # Endregion
-
- PrivateValueProviderResultGetTagsValue ()
- {
- StringstrTags=GetValueFromRequest("Tags ");
- If (string. IsNullOrEmpty (strTags ))
- {
- Returnnull;
- }
-
- String []Tags=StrTags. Split (newstring [] {","}, StringSplitOptions.
RemoveEmptyEntries );
- StateList<String>TagsList=NewStateList<String>();
- Foreach (stringtagintags)
- {
- TagsList. Add (tag. Trim (). ToLowerInvariant ());
- }
-
- ReturnnewValueProviderResult (tagsList, strTags, CultureInfo.
InvariantCulture );
- }
-
- PrivateValueProviderResultGetCategoriesValue ()
- {
- StringstrCategories=GetValueFromRequest("Categories ");
- If (string. IsNullOrEmpty (strCategories ))
- {
- Returnnull;
- }
-
- String []Categories=StrCategories. Split (newstring [] {","},
StringSplitOptions. RemoveEmptyEntries );
- StateList<Category>List=NewStateList<Category>();
- Foreach (stringcincategories)
- {
- List. Add (Category. GetCategory (newGuid (c )));
- }
-
- ReturnnewValueProviderResult (list, strCategories, CultureInfo. InvariantCulture );
- }
-
- PrivatestringGetValueFromRequest (stringname)
- {
- Stringvalue=Null;
- HttpRequestBaserequest=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];
- }
- }
-
- Returnvalue;
- }
- }
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")]
- PublicActionResultSaveNewPost (FormCollectionform)
- {
- Postpost=NewPost();
- Try
- {
- UpdateModel (post, new [] {"Title", "Content", "Slug", "Tags", "Categories "},
NewPostValueProvider (ControllerContext ));
- }
- Catch
- {
- ReturnView (post );
- }
-
- ..
- }
The above describes how to use the UpdataModel method in ASP. net mvc.
- Introduction to ASP. NET 2.0 Virtual Hosts
- Introduction to ASP. NET Applications
- Optimize ASP. NET 2.0 Profile Provider
- ASP. NET pipeline Optimization
- Introduction to ASP. NET Routing Engine