如何从viewmodel进行验证

时间:2016-06-06 18:49:05

标签: asp.net-mvc-4 c#-4.0

我有一个模型,下面有一些验证

        public class RequestABook
        {
            [Required(ErrorMessage = "Name Required")]
            [DisplayName("Book Name")]
            public string BookName { get; set; }

            [Required(ErrorMessage = "Zipcode Required")]
            [DisplayName("Zipcode")]
            public string ZipCode { get; set; }

            [Required(ErrorMessage = "Contact Name Required")]
            [DisplayName("Contact Name")]
            public string ContactName { get; set; }

            [Required(ErrorMessage = "Email Id Required")]
            [DataType(DataType.EmailAddress)]
            public string Email { get; set; }

            [Required(ErrorMessage = "Book Description Required")]
            public string BookDescription { get; set; }

            [Required(ErrorMessage = "You need to check one answer")]
            public string Answer { get; set; }
        }

我这里有一个视图模型

        public class RequestViewModel
        {
            public RequestABook MyTestViewModel { get; set; }   
        }

我的主页面加载了

        @model BookRequestValidation.Models.RequestViewModel

        @{
            ViewBag.Title = "RequestABook";
        }

        <h2>RequestABook</h2>

        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>Request Book</legend>
                <div class="editor-label">
                    @Html.LabelFor(m => m.MyTestViewModel.BookName)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(m => m.MyTestViewModel.BookName)
                    @Html.ValidationMessageFor(m => m.MyTestViewModel.BookName)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(m => m.MyTestViewModel.ZipCode)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(m => m.MyTestViewModel.ZipCode)
                    @Html.ValidationMessageFor(m => m.MyTestViewModel.ZipCode)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(m => m.MyTestViewModel.ContactName)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(m => m.MyTestViewModel.ContactName)
                    @Html.ValidationMessageFor(m => m.MyTestViewModel.ContactName)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(m => m.MyTestViewModel.Email)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(m => m.MyTestViewModel.Email)
                    @Html.ValidationMessageFor(m => m.MyTestViewModel.Email)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(m => m.MyTestViewModel.BookDescription)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(m => m.MyTestViewModel.BookDescription)
                    @Html.ValidationMessageFor(m => m.MyTestViewModel.BookDescription)
                </div>

                <div id="HCBudget" class="validation">
                    <label for="budgethealth">Budget Health</label>
                    @Html.RadioButton("Answer", "Red")
                    @Html.RadioButton("Answer", "Yellow")
                    @Html.RadioButton("Answer", "Green")  
                    @Html.ValidationMessageFor(m => m.MyTestViewModel.Answer)   
                </div>

                <input type="submit" value="Request Book" />
            </fieldset> 
            } 

问题:你们如何使用viewmodel中使用的模型处理验证。 在我在viewmodel中使用它之前,一切都运行良好。当我使用viewmodel时 验证停止了工作。

以下是帖子的动作。

        public ActionResult RequestABook()
        {
            return View();
        }

        [HttpPost]
        public ActionResult RequestABook(RequestABook quote)
        {
            return View();
        }

1 个答案:

答案 0 :(得分:0)

如果您发布了POST动作,那将会非常有用。但是,一般来说,我可以说验证只在相关的类实例上运行,如果它们是非空的。因此,除非为MyTestViewModel上的至少一个属性发布值,否则模型绑定器不会对其进行实例化(MyTestViewModel将为null),并且对其属性进行验证将不要跑了。

您可以通过始终通过视图模型的构造函数实例化MyTestViewModel属性来修复此方案,或者更好的方法是使用自定义getter和setter:

  1. 构造

    public class RequestViewModel
    {
        public RequestViewModel()
        {
            MyTestViewModel = new RequestABook();
        }
    
        ...
    }
    
  2. 自定义Getter和Setter

    private RequestABook myTestViewModel;
    public RequestABook MyTestViewModel
    {
        get
        {
            if (myTestViewModel == null)
            {
                myTestViewModel = new RequestABook();
            }
            return myTestViewModel;
        }
        set { myTestViewModel = value; }
    }