问题描述:

I would like to add a textbox (date) and button to my report, which filters the data.

The below mvc is working, but the input must still be validated (must be a DATE) on client side (and server side if possible)

My Model looks like this :

public class DailyReport

{

public int DailyReportID { get; set; }

public DateTime? ReportDate { get; set; }

}

View :

@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)

}

my controller:

 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());

}

Can someone help me please?

How do I make sure a valid date is entered in the textbox?

Should I create a another model with a date field for this input?

网友答案:

Utilize the DataTypeAttribute from the DataAnnotations namespace in your Model, like so:

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; }
}

In your View, have something like:

@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)
}
网友答案:

@Shark Shark pointed me in the right direction to use a viewmodel, this is the end result that is now working. JS validation added as well.

(DBSet was not necessary because DrViewModel is a viewmodel.)

controllers :

    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);
    }

models :

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; }
}

view :

@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)

    }
相关阅读:
Top