IValueProvider the new features of ASP.NET MVC Beta

In the newly released ASP.NET MVC Beta version, providing a heavy load with IValueProvider parameter in UpdataModel method. Well, this IValueProvider what use is it?

Let's look at a simple scenario, such as our blog system has a target of Post, Post object has a property Tags and Categories property, their types are:

Post.Tags: StateList <string> (a List <T> is the extended type BlogEngine.NET)
Post.Categories: statelist <the Category>

 

If we want to use UpdataModel method to update to our Post over Form form data to our Post subject, there may be the following code:

 

/// <summary>
Save the new content // Essay form submitted to the database over
/// </ Summary>
[AcceptVerbs ( "the POST"), ActionName ( "newPost")]
public ActionResult SaveNewPost (the FormCollection form)
{
    POST POST = new new Post ();
    the try
    {
        the UpdateModel (POST, new new [] { "the Title", "the Content", "of Slug", "Tags", "the Categories"});
    }
    the catch
    {
        return View (POST);
    } 
    ..

Copy the code

Obviously, in the above code, we use UpdateModel to update the Tags and Categories property when it is unlikely to be successful because UpdateModel method does not know how to submit Form over the "Tags" and "Categories" data into StateList < string> type and stateList <Category> type. This time we need to provide a ValueProvider, to carry out this conversion.

To achieve a ValueProvider, we only need to implement GetValue method IValueProvider interface, and returns the result of a ValueProviderResult on it. Here we write a PostValueProvider to the achievement of our proposed above. code show as below:


public class PostValueProvider : IValueProvider
{
    private ControllerContext context;
    //private DefaultValueProvider dProvider; 

    public PostValueProvider(ControllerContext context)
    {
        this.context = context;
        //dProvider = new DefaultValueProvider(context);
    } 

    #region IValueProvider 成员 

    public ValueProviderResult GetValue(string name)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentException("参数不能为空", "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 UpdateModel method:

 

Copy the code

/// <summary>
Save the new content // Essay form submitted to the database over
/// </ Summary>
[AcceptVerbs ( "the POST"), ActionName ( "newPost")]
public ActionResult SaveNewPost (the FormCollection form)
{
    POST POST = new new Post ();
    the try
    {
        the UpdateModel (POST, new new [] { "the Title", "the Content", "of Slug", "Tags", "the Categories"}, new new PostValueProvider (the ControllerContext));
    }
    the catch
    {
        return View (POST);
    } 

   ..

Copy the code

Enjoy!Post by Q.Lee.lulu.

If you want to learn more about ASP.NET MVC, you can reference ASP.NET MVC Getting Started series , we can also sample code from ASP.NET MVC series on the download.

Published 720 original articles · won praise 71 · views 90000 +

Guess you like

Origin blog.csdn.net/sinolover/article/details/104376268