-1

I'm new to model bindingcontext and can't understand why I keep getting null on result

As far as I understand I need to implement IUnvalidatedValueProvider in order to turn off validation (as users are posting 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;
    }
}
4

2 回答 2

0

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

public class MyViewModel
{
    ...
    [AllowHtml]
    [DataType(DataType.Html)]
    public string Content { get; set; }
    ...
}
于 2013-06-24T19:23:45.790 回答
0

我最后做到了

HttpRequestBase request = controllerContext.HttpContext.Request;
var re = request.Unvalidated("CMessage");
于 2013-06-24T19:50:24.230 回答