我如何根据元数据验证在Razor中生成日期字段

时间:2016-07-06 10:16:42

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

我有一个这样的代码,我将在我的视图中验证

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using ValidationProject.Models;


namespace ValidationProject.Models
{
    public class Class1
    {
        [Required(ErrorMessage="This is field is required")]
        public int one;

        [Required(ErrorMessage = "This is field is required")]
        public string two;

        [DataType(DataType.Date)]
        public DateTime date;
    }
}

当我尝试在剃刀中生成日期时间字段时,我只获取文本框而不是日期字段。 Razor代码如下:

@using ValidationProject.Models;
@model Class1
@{
    <script src="@Url.Content("~/Scripts/jquery-3.0.0.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"></script>
    <script src="~/Scripts/bootstrap.js"></script>
    <link href="~/Content/Site.css" rel="stylesheet" />
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-theme.css" rel="stylesheet" />

    <link href="~/Content/Site.css" rel="stylesheet" />
    ViewBag.Title = "Home Page";

    <script type="text/javascript">
        $(function () {
            $("input").bind("input propertychange", function () {
                if($(this).not("field-validation-error"))
                {
                    $(this).addClass("success");

                }
            });
        });
    </script>

    <style type="text/css">
        .input-validation-error {
            border: 3px solid red;
            background-color:  #fee;
        }
        .field-validation-error{
            background:url("@Url.Content("~/Content/1.png")") no-repeat;
            font-size:15px;
            background-size:25% 100%;
            padding-left:60px;
            padding-top:15px;
            padding-bottom:15px;
            color:black;
        }
    </style>
}
@{Html.EnableClientValidation();}
@{Html.EnableUnobtrusiveJavaScript();}

 @using (@Html.BeginForm())
 {
     @Html.EditorFor(m=>m.date)
     <input type="submit" />

 }

我使用@Html.EditorFor(m=>m.date)来生成剃须刀中的日期,但它无法做到。 请帮忙。

0 个答案:

没有答案