对剃刀视图MVC的搜索输入/文本框的日期验证

时间:2012-02-10 13:25:46

标签: asp.net-mvc-3 razor validation

我想在报告中添加一个文本框(日期)和按钮,用于过滤数据。

以下mvc正在运行,但仍必须在客户端(如果可能,服务器端)验证输入(必须是DATE)

我的模型看起来像这样:

public class DailyReport
{    
    public int DailyReportID { get; set; }
    public DateTime? ReportDate { get; set; }
}

查看:

@model IEnumerable<project_name.Models.DailyReport>
@* text box and button:  *@
@using (Html.BeginForm("Index", "DailyReport", FormMethod.Get))
{   <p>
     Title: @Html.TextBox("SearchDateString")
     <input type="submit" value="Filter" />
     </p>
}
@* display dates*@
@foreach (var item in Model)
{       @Html.DisplayFor(modelItem => item.ReportDate)
}

我的控制员:

   public ViewResult Index(String SearchDateString)
    {
        var dailyreport = db.DailyReport.Include(d => d.Site);

        if (!String.IsNullOrEmpty(SearchDateString))
        {
            DateTime search_date = Convert.ToDateTime(SearchDateString);
            dailyreport = dailyreport.Where(r => r.ReportDate == search_date);
        }
        return View(dailyreport.ToList());
    }
是的,有人能帮帮我吗? 如何确保在文本框中输入有效日期? 我应该为此输入创建另一个带有日期字段的模型吗?

2 个答案:

答案 0 :(得分:3)

利用模型中DataTypeAttribute命名空间的DataAnnotations,如下所示:

public class DailyReport
{    
    public int DailyReportID { get; set; }
    public DateTime? ReportDate { get; set; }
}

public class DrViewModel
{
    [DataType(DataType.Date)]
    public string DateTimeSearch { get; set; }
    List<DailyReport> DailyReports { get; set; }
}

在您的视图中,有类似的内容:

@model project_name.Models.DrViewModel

@using (Html.BeginForm("Index", "DailyReport", FormMethod.Get))
{   
     <p>
     Title: @Html.TextBoxFor(m => m.DateTimeSearch)
     <input type="submit" value="Filter" />
     </p>
}


@foreach (var item in Model.DailyReports)
{
    @Html.DisplayFor(m => item.ReportDate)
}

答案 1 :(得分:0)

@Shark Shark指出我使用viewmodel的正确方向,这是现在正在运行的最终结果。 JS验证也加入了。

(因为DrViewModel是一个视图模型,所以不需要DBSet。)

控制器:

    public ActionResult Index(DrViewModel dvm)
    {
        var dailyreport = db.DailyReport.Include(d => d.Site);
        if (dvm.DateTimeSearch != null)
        {
            dailyreport = dailyreport.Where(r => r.ReportDate == dvm.DateTimeSearch);
        }

        dvm.DailyReport = dailyreport.ToList();
        return View(dvm);
    }

模特:

public class DrViewModel
{
    public DateTime? DateTimeSearch { get; set; }
    public List<DailyReport> DailyReport { get; set; }
}

 public class DailyReport
{

    public int DailyReportID { get; set; }
     public DateTime? ReportDate { get; set; }
}

观点:

@model myproject.Models.DrViewModel

@{
ViewBag.Title = "Index";
 }

<h2>Index</h2>
<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>

@using (Html.BeginForm("Index","DailyReport", FormMethod.Get ))
{   
  @Html.ValidationSummary(true)

 <div class="editor-label">
        @Html.LabelFor(model => model.DateTimeSearch)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DateTimeSearch)
        @Html.ValidationMessageFor(model => model.DateTimeSearch)
    <input type="submit" value="Filter" />
    </div>

}    
   @foreach (var item in Model.DailyReport)
   {
                @Html.DisplayFor(modelItem => item.ReportDate)

    }