MVC3自定义服务器端数据注释验证在部分视图中不起作用

时间:2012-10-12 16:44:26

标签: asp.net-mvc-3 data-annotations partial-views custom-attributes

我正在尝试通过数据注释添加自定义数据验证。我现在只关心服务器端验证。我在这里和其他地方看到的所有内容都引用了客户端验证。我已经删除了所有代码,运行了一些测试用例,我可以在常规视图中正常工作但是只要表单处于局部视图中,代码就不再在方法中中断以覆盖IsValid。

在任何一种情况下,我都可以看到自定义属性被初始化。当表单处于常规视图中时,我可以看到在提交表单时执行的覆盖方法,但是在部分视图中,代码永远不会被执行,而是直接进入HttpPost操作。

我花了两天的时间来试图解决这个问题,但我感到很茫然。任何帮助都会非常感激。

注意: 下面的代码在进入HttpPost操作时会返回相同的视图。我有这样的测试用途。我知道我的覆盖永远不会从局部视图调用,因此IsValid始终是真的。

查看显示验证工作的表单

@model eRecruitBoard.ViewModels.HomeIndexViewModel
@{
    ViewBag.Title = "eRecruitBoard";
}
@*<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>*@

<h2>Login/homepage</h2>

<br /><br />
 <div class="errorMessage">
 @Html.DisplayFor(m => m.LoginErrorMsg)
</div>
<br />

@using (Html.BeginForm("Index", "Home")) {
    <div id="loginControlBox">
        <fieldset>
            <legend>Welcome to eRecruitBoard</legend> 

            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </div>

            <div class="editor-label">
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe)
            </div>
            <div class="editor-label">
                @Html.EditorFor(m => m.Date)
                @Html.ValidationMessageFor(m => m.Date)
            </div>
            <p>
                <input type="submit" value="Log In" />
            </p>
        </fieldset>
    </div>
}
    <div>
        @Html.Action("BlankForm", "TestForm")
    </div>

部分视图(这些脚本调用也来自_Layout,但我也在这里进行测试)

@model eRecruitBoard.ViewModels.TestFormViewModel

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>    
    <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script>

@using (Html.BeginForm("NewActivity2", "TestForm", FormMethod.Post))
{
<fieldset>
    <legend>Test Form</legend>
    <br />
    @Html.LabelFor(m => m.Date)
    @Html.EditorFor(m => m.Date)
    @Html.ValidationMessageFor(m => m.Date)
    <input id="activityTimelineSubmit" type="submit" value="Submit" />
</fieldset>
}

ViewModel(部分视图)

namespace eRecruitBoard.ViewModels
{
    public class TestFormViewModel
    {
        [Required]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
        [NonFutureDate()]
        [Display(Name = "Date")]
        public DateTime Date { get; set; }
    }
}

控制器(部分视图)

namespace eRecruitBoard.Controllers
{
    public class TestFormController :BaseController
    {
        public ActionResult BlankForm()
        {
            var viewModel = new TestFormViewModel
            {
                Date = DateTime.Today
            };

            return PartialView("_TestForm", viewModel);
        }

        [HttpPost]
        public ActionResult NewActivity2(DateTime Date)
        {
            if (!ModelState.IsValid)
                return RedirectToAction("Index", "Home");
            else
                return RedirectToAction("Index", "Home");
        }
    }
}

验证码

using System;
using System.ComponentModel.DataAnnotations;

namespace eRecruitBoard.WebLibrary.Validation
{
    [AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
    public class NonFutureDateAttribute : ValidationAttribute  //public sealed class 
    {
        public NonFutureDateAttribute(): base("Activity can only be saved for today or dates in the past.")
        {
        }
        public override bool IsValid(object value)
        {
            DateTime dateToCheck = (DateTime)value;
            return (dateToCheck <= DateTime.Today);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如果您RedirectToAction丢失了所有验证。您必须在POST操作中return PartialView(model)。这需要将NewActivity2的参数类型更改为TestFormViewModel而不是DateTime

使用如何显示部分视图(@Html.Partial()或javascript)的示例更新代码,如果它仍然无效。