Custom Model Binder中的DataAnnotations验证

时间:2010-08-31 18:08:33

标签: validation asp.net-mvc-2 data-annotations custom-model-binder

我已经在我的MVC项目中为List编写了一个自定义模型绑定器但是我现在陷入了如何让这个绑定器根据我的DataAnnotations验证属性进行验证的问题。

我在互联网上发现了一些关于类似场景的帖子,但我找不到任何适用于我特定场景的内容。

模型活页夹代码:

    public class QuestionModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        List<QuestionEditModel> res = new List<QuestionEditModel>();
        var form = controllerContext.HttpContext.Request.Form;
        int i = 0;
        while (!string.IsNullOrEmpty(form["Questions[" + i + "].QuestionID"]))
        {
            var p = new QuestionEditModel();
            p.QuestionID = form["Questions[" + i + "].QuestionID"];
            p.Answer = form["Questions[" + i + "].Answer"];
            p.AnswerRequired = (form["Questions[" + i + "].AnswerRequired"] == "True") ? true : false;
            p.completedBy = form["Questions[" + i + "].completedBy"];
            p.completedOn=NullableParser.ParseNullableDateTime(form["Questions[" + i + "].CompletedOn"]);
            p.DefaultText = form["Questions[" + i + "].DefaultText"];
            p.EntryType = form["Questions[" + i + "].EntryType"];
            p.HelpText = form["Questions[" + i + "].HelpText"];
            p.OptionRequired = (form["Questions[" + i + "].OptionRequired"] == "True") ? true : false;
            p.OptionValue = NullableParser.ParseNullableInt(form["Questions[" + i + "].OptionValue"]);
            p.QuestionName = form["Questions[" + i + "].QuestionName"];
            p.QuestionText = form["Questions[" + i + "].QuestionText"];
            res.Add(p);
            i++;
        }

        return res;
    }

    private bool HasGenericTypeBase(System.Type type, System.Type genericType)
    {
    while (type != typeof(object))
    {
        if (type.IsGenericType && type.GetGenericTypeDefinition() == genericType) return true;
        type = type.BaseType;
    }

    return false;        
    }

}

我的模型MetaData:

    [MetadataType(typeof(QuestionEditModelMetaData))]
public partial class QuestionEditModel { }


public class QuestionEditModelMetaData
{
    [Required]
    public string QuestionID { get; set; }
    [Required]
    public string QuestionName { get; set; }
    [Required]
    public string QuestionText { get; set; }
    [Required]
    public string DefaultText { get; set; }
    [Required]
    public string EntryType { get; set; }
    [Required]
    public string HelpText { get; set; }

    public Boolean AnswerRequired { get; set; }
    public Boolean OptionRequired { get; set; }

    //[RequiredIfTrue("AnswerRequired")]
    [Required]
    public string Answer { get; set; }

    [RequiredIfTrue("OptionRequired")]
    public int? OptionValue { get; set; }

    public string completedBy { get; set; }
    public DateTime? completedOn { get; set; }

    public List<Option> options { get; set; }

}

RequiredIfTrue属性来自MVC Foolproof Validation库。显然它不够万无一失!

1 个答案:

答案 0 :(得分:2)

此处的解决方案有效。

Custom model binding, model state, and data annotations

CreateModel选项让生活变得非常轻松。