相关疑难解决方法(0)

在自定义模型绑定器中设置ModelState值

我在ASP.NET MVC 2中使用自定义模型绑定器,如下所示:

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }

        BaseContentObject obj = (BaseContentObject)base.BindModel(controllerContext, bindingContext);
        if(string.IsNullOrWhiteSpace(obj.Slug))
        {
            // creating new object
            obj.Created = obj.Modified = DateTime.Now;
            obj.ModifiedBy = obj.CreatedBy = controllerContext.HttpContext.User.Identity.Name;
            // slug is not provided thru UI, derivate it from Title; property setter removes chars that are not allowed
            obj.Slug = obj.Title;
            ModelStateDictionary modelStateDictionary = bindingContext.ModelState;
            modelStateDictionary.SetModelValue("Slug", new ValueProviderResult(obj.Slug, obj.Slug, …
Run Code Online (Sandbox Code Playgroud)

modelbinders asp.net-mvc-2

15
推荐指数
1
解决办法
9996
查看次数

标签 统计

asp.net-mvc-2 ×1

modelbinders ×1