In the newly released ASP.net MVC beta release, an overload with Ivalueprovider parameters is provided in the Updatamodel method. So what's the use of this ivalueprovider?
Let's take a look at a simple scenario, such as our blog system has a post object, the Post object has a tags attribute and a categories attribute, and their types are:
Post.tags:statelist<string> (an extended type of list<t> in BlogEngine.NET)
Post.categories:statelist<category>
If we were to use the Updatamodel method to update the form data we post to our post object, we might have the following code:
/// <summary>
/// 将提交过来的新随笔表单内容保存到数据库
/// </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, we use Updatemodel to update Tags and Categories properties, it is impossible to succeed, because the Updatemodel method does not know how to submit form to come over the "Tags" and "Categories" Data is converted to statelist<string> type and statelist<category> type. At this point we need to provide a valueprovider for this transformation.
To implement a valueprovider, we only need to implement the GetValue method of the Ivalueprovider interface and return a valueproviderresult result. Below we will write a postvalueprovider to achieve the above we put forward the situation. The code is as follows:
Postvalueprovider
public class Postvalueprovider:ivalueprovider
{
private ControllerContext context;
//private Defaultvalueprovider 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 null", "name");
}
switch (name)
{
case "Tags":
return Gettagsvalue ();
case "Categories":
return Getcategoriesvalue ();
Default:
return to 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 to 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 to 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;
}
}