The ivalueprovider of ASP.net MVC's new beta features

Source: Internet
Author: User

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&lt;string&gt; tagslist = new statelist&lt;string&gt; ();


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&lt;category&gt; list = new statelist&lt;category&gt; ();


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) &amp;&amp; (request. Form!= null))


      {


value = Request. Form[name];


      }


    }


return value;


  }


}

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.