ASP.NET MVC ModelBindingContext问题

时间:2013-06-24 19:16:47

标签: asp.net-mvc

我是模特bindingcontext的新手,无法理解为什么我会继续null获得结果

据我了解,我需要实现IUnvalidatedValueProvider以关闭验证(因为用户发布了html)。

public class AccountFormBinder : IModelBinder 
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        //Cast the value provider to an IUnvalidatedValueProvider, which allows to skip validation
        IUnvalidatedValueProvider provider = bindingContext.ValueProvider as IUnvalidatedValueProvider;
        var result = provider.GetValue(bindingContext.ModelName,true);
        return result.AttemptedValue;
    }
}

2 个答案:

答案 0 :(得分:0)

那么,您只是想允许发布HTML?您不需要自定义模型绑定器。只需将[AllowHtml]添加到应允许包含帖子值的HTML的任何属性中:

public class MyViewModel
{
    ...
    [AllowHtml]
    [DataType(DataType.Html)]
    public string Content { get; set; }
    ...
}

答案 1 :(得分:0)

我最后这样做了

HttpRequestBase request = controllerContext.HttpContext.Request;
var re = request.Unvalidated("CMessage");