为什么我的模型无法通过验证 [英] Why is my model failing validation

查看:61
本文介绍了为什么我的模型无法通过验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次加载页面时,两个日期文本框均未通过验证.这似乎是因为两个Date属性均按要求设置,但它们为空.

我的目标是:
1)有一个要传递到控制器的模型,其中包含要搜索的条件.
2)页面返回结果时,将重新加载该条件.
3)第1页加载时,它将日期设置为默认的 DateTime.Now ,并且不显示任何结果.提交条件时,它将在下一页加载时显示结果.

 //型号公共类SearchModel{公共长?StudentId {获取;放;}[必需的]公开DateTime?Date1 {get;放;}[必需的]公开DateTime?Date2 {get;放;}公共列表< string>学生{得到;放;}}//看法@model搜索模型< div>@using(Html.BeginForm("StudentSearch","Student",FormMethod.Post)){< span>日期1 @ Html.TextBoxFor(m => m.Date1)日期2 @ Html.TextBoxFor(m => m.Date2)<输入type ="submit" value =搜索"/></span>}</div>< div>@foreach(模型中的var.学生){< span> @ s</span>}</div>//控制器[HttpGet]公共ActionResult StudentSearch(SearchModel模型){如果(model.Date1 == null || model.Date2 == null){model.Date1 = DateTime.Now;model.Date2 = DateTime.Now;}返回View();} 

解决方案

日期时间输入非常敏感.用户可以输入错误,并且 ModelBinder 无法将其绑定到参数.因此,建议您使用

 公共类StudentController:控制器{[HttpGet]公共ActionResult StudentSearch(SearchModel模型){如果(model.Date1 == null || model.Date2 == null){model.Date1 = DateTime.Now;model.Date2 = DateTime.Now;}返回View(model);< =====}[HttpPost]公共ActionResult StudentSearchPost(SearchModel模型){如果(ModelState.IsValid){//做一点事}返回View();}} 

查看

  @model DemoWebMvc.Models.SearchModel< div>@using(Html.BeginForm("StudentSearchPost","Student",FormMethod.Post)){< span>日期1 @ Html.TextBoxFor(m => m.Date1)日期2 @ Html.TextBoxFor(m => m.Date2)<输入type ="submit" value =搜索"/></span>}</div> 

来自评论: 我正在使用日期时间选择器控件.自模型默认为NULL,因为它是DateTime? 第一页加载显示我的日期时间字段未通过验证. 他们没有失败,因为用户选择的内容无效

问题是您在页面加载时将无效模型传递给View,并且默认模型绑定程序尝试绑定到模型实例,并且在页面加载时触发了验证错误.

如果您没有有效的模型,则不应将其发送给页面加载查看,以免显示验证错误.

 公共类StudentController:控制器{[HttpGet]公共ActionResult StudentSearch(){返回View();}[HttpPost]公共ActionResult StudentSearch(SearchModel模型){如果(ModelState.IsValid){}返回View(model);}} 

查看

  @model DemoWebMvc.Models.SearchModel@ {布局= null;}@using(Html.BeginForm("StudentSearch","Student",FormMethod.Post)){@ Html.ValidationSummary(true)< span>日期1 @ Html.TextBoxFor(m => m.Date1)@ Html.ValidationMessageFor(m => m.Date1)br/>日期2 @ Html.TextBoxFor(m => m.Date2)@ Html.ValidationMessageFor(m => m.Date2)br/><输入type ="submit" value =搜索"/></span>}< script src =〜/Scripts/jquery-1.10.2.min.js"></script>< script src =〜/Scripts/jquery.validate.min.js"></script>< script src =〜/Scripts/jquery.validate.unobtrusive.min.js"></script>< script src ="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>< script>$(函数(){$(#Date1").datepicker();$(#Date2").datepicker();});</script> 

When I first load the page both my date textboxes are failing validation. This appears to be because the two Date properties are set as required but are null.

My goals are to:
1) Have a model to pass into the controller that contains the criteria to search for.
2) That criteria will get reloaded when the page returns along with the results.
3) When the page 1st loads it will set the dates to a default to DateTime.Now and NOT show any results. When you submit the criteria it will then show results on next page load.

// Model
public class SearchModel
{
    public long? StudentId { get; set; }

    [Required]
    public DateTime? Date1 { get; set; }

    [Required]
    public DateTime? Date2 { get; set; }

    public List<string> Students { get; set; }
}

// View
@model SearchModel
<div>
    @using (Html.BeginForm("StudentSearch", "Student", FormMethod.Post))
    {
        <span>
            Date 1 @Html.TextBoxFor(m => m.Date1)
            Date 2 @Html.TextBoxFor(m => m.Date2)
            <input type="submit" value="Search" />
        </span>
    }
</div>
<div>
    @foreach(var s in model.Students)
    { <span>@s</span> }
</div>

// Controller
[HttpGet]
public ActionResult StudentSearch(SearchModel model)
{
    if (model.Date1 == null || model.Date2 == null)
    {
        model.Date1 = DateTime.Now;
        model.Date2 = DateTime.Now;
    }

    return View();
}

解决方案

Date time input is very sensitive. User could make a typo and ModelBinder won't be able to bind it to parameter. So, I suggest you to use framework like jQuery UI Datepicker or Kendo Datepicker.

public class StudentController : Controller
{
    [HttpGet]
    public ActionResult StudentSearch(SearchModel model)
    {
        if (model.Date1 == null || model.Date2 == null)
        {
            model.Date1 = DateTime.Now;
            model.Date2 = DateTime.Now;
        }
        return View(model); <=====
    }

    [HttpPost]
    public ActionResult StudentSearchPost(SearchModel model)
    {            
        if (ModelState.IsValid)
        {
            // Do something
        }
        return View();
    }
}

View

@model DemoWebMvc.Models.SearchModel

<div>
    @using (Html.BeginForm("StudentSearchPost", "Student", FormMethod.Post))
    {
        <span>
            Date 1 @Html.TextBoxFor(m => m.Date1)
            Date 2 @Html.TextBoxFor(m => m.Date2)
            <input type="submit" value="Search"/>
        </span>
    }
</div>

From Comment: I'm using a datetime picker control. Since the model defaults to NULL since it's a DateTime? the very first page load shows my date time fields failing validation. They're not failing because what the user selects is invalid

The problem is you pass invalid model to View at Page Load, and default model binder tries to bind to model instance and it triggers validation error at Page Load.

If you do not have valid model, you should not send it to view at Page Load to avoid displaying validation error.

public class StudentController : Controller
{
    [HttpGet]
    public ActionResult StudentSearch()
    {
        return View();
    }

    [HttpPost]
    public ActionResult StudentSearch(SearchModel model)
    {
        if (ModelState.IsValid)
        {
        }
        return View(model);
    }
}

View

@model DemoWebMvc.Models.SearchModel

@{
    Layout = null;
}

@using (Html.BeginForm("StudentSearch", "Student", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <span>
        Date 1 @Html.TextBoxFor(m => m.Date1)
        @Html.ValidationMessageFor(m => m.Date1)<br />
        Date 2 @Html.TextBoxFor(m => m.Date2)
        @Html.ValidationMessageFor(m => m.Date2)<br />
        <input type="submit" value="Search" />
    </span>
}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script>
    $(function () {
        $("#Date1").datepicker();
        $("#Date2").datepicker();
    });
</script>

这篇关于为什么我的模型无法通过验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆