要实现一个ValueProvider,我们只需要实现IValueProvider接口的GetValue方法,并且返回一个ValueProviderResult的结果就可以了。下面我们就写一个PostValueProvider来实现上面我们提出的情况。代码如下:
PostValueProvider
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;
}
}
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;
}
}
然后我们就可以在UpdateModel方法中使用我们的PostValueProvider了:
/// <summary>
/// 将提交过来的新随笔表单内容保存到数据库
/// </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);
}
..
}
/// 将提交过来的新随笔表单内容保存到数据库
/// </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);
}
..
}