2017-09-14 92 views
0

祝大家晚上好,我希望任何人都可以在视图部分帮助我使用日期时间范围过滤器。这里是我的模型:asp.net mvc 5中的日期范围搜索过滤器功能?

public class Student 
{ 
    public int ID { get; set; } 
    public string StudentName { get; set; } 
    public int CourseId { get; set; } 
    public virtual Course Course { get; set; } 
    public DateTime CurrentDate { get; set; } 
    public Student() 
    { 
     CurrentDate = DateTime.Now; 
    } 
} 

我使用视图模型显示,现在这里是我的控制器:

public ActionResult Index(DateTime? startdate, DateTime? enddate) 
    { 
     var rangeData = db.Students.Where(x => x.CurrentDate >= startdate && x.CurrentDate <= enddate).ToList(); 

     return View(rangeData); 
    } 

现在我有一些问题与观点,以及在控制器。

这是我的问题:如何将开始日期和结束日期传递给控制器​​以获取具有定义属性的订单?这是我的观点,我做错了什么?

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 

@using (Html.BeginForm("Index", "Students", FormMethod.Get)) 
{ 
    <fieldset> 
     <legend>Search criteria</legend> 
     @Html.Label("StartDate", "Start Date:") 
     <input class="startdate" id="startdate" name="startdate" type="date" value=""> 
     @Html.Label("enddate", "End Date:") 
     <input class="startdate" id="enddate" name="enddate" type="date" value=""> 
     <input type="submit" value="Apply" /> 
    </fieldset> 
} 


<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.StudentName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Address) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Gender) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.MobileNo) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Course) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.StudentName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Address) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Gender) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.MobileNo) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Course.CourseName) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | 
      @Html.ActionLink("Details", "Details", new { id=item.ID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 
     </td> 
    </tr> 
} 

</table> 
+0

为什么你有2个独立的表格和第二个控制器的方法。您的所有控件应该采用一种形式并发布到第一种方法。并且不要创建像这样的输入。使用带有这些属性的视图模型('SearchBy','StartDate'等等,并用'List '属性为过滤的集合 –

+0

请问您可以通过代码在控制器中发布并查看 –

+0

确切的问题和控制器? – Ravi

回答

0

提供了一个占位符属性你输入字段,以指导用户预期的日期格式,你可以使用,因为这似乎是一个日期格式问题的一个选项。另一种选择是使用日期选择器控件,该控件将以正确的格式自动设置日期。

不过,如果你想给你的用户灵活地输入的日期在自己选择的任何分离的任何格式,包括“/”,“ - ”,或只是篇幅,这里是一些小费

 public ActionResult Index(string startdate = null, string enddate = null) 
    { 
     if (startdate != null && enddate != null) 
     { 
      //this will default to current date if for whatever reason the date supplied by user did not parse successfully 

      DateTime start = DateManager.GetDate(startdate) ?? DateTime.Now; 

      DateTime end = DateManager.GetDate(enddate) ?? DateTime.Now; 

      var rangeData = db.Students.Where(x => x.CurrentDate >= start && x.CurrentDate <= end).ToList(); 

      return View(rangeData); 
     } 
     return View(); 
    } 


     public class DateManager 
    { 
     /// <summary> 
     /// Use to prevent month from being overritten when day is less than or equal 12 
     /// </summary> 
     static bool IsMonthAssigned { get; set; } 



     public static DateTime? GetDate(string d) 
     { 
      char[] splitsoptions = { '/', '-', ' ' }; 
      foreach (var i in splitsoptions) 
      { 
       var y = 0; 
       var m = 0; 
       var day = 0; 
       if (d.IndexOf(i) > 0) 
       { 
      try{ 
        foreach (var e in d.Split(i)) 
        { 


         if (e.Length == 4) 
         { 
          y = Convert.ToInt32(e); 

          continue; 
         } 
         if (Convert.ToInt32(e) <= 12 && !IsMonthAssigned) 
         { 
          m = Convert.ToInt32(e); 
          IsMonthAssigned = true; 
          continue; 
         } 
         day = Convert.ToInt32(e); 


        } 

        return new DateTime(y, m, day); 
      }catch 
      { 
      //We are silent about this but we could set a message about wrong date input in ViewBag and display to user if this this method returns null 
      } 
       } 
      } 
      return null; 


     } 
    // Another overload. this will catch more date formats without manually checking as above 

    public static DateTime? GetDate(string d, bool custom) 
     { 
      CultureInfo culture = new CultureInfo("en-US"); 

      string[] dateFormats = 
      { 
       "dd/MM/yyyy", "MM/dd/yyyy", "yyyy/MM/dd", "yyyy/dd/MM", "dd-MM-yyyy", "MM-dd-yyyy", "yyyy-MM-dd", 
       "yyyy-dd-MM", "dd MM yyyy", "MM dd yyyy", "yyyy MM dd", "yyyy dd MM", "dd.MM.yyyy", "MM.dd.yyyy", 
       "yyyy.MM.dd", "yyyy.dd.MM","yyyyMMdd","yyyyddMM","MMddyyyy","ddMMyyyy" 
      };//add your own to the array if any 

      culture.DateTimeFormat.SetAllDateTimePatterns(dateFormats, 'Y'); 

      if (DateTime.TryParseExact(d, dateFormats, culture, DateTimeStyles.None, out var date)) 
       return date; 

      return null; 


     } 
    } 
+0

显示信息,如“显示结果为”选择日期范围“将使用户可以看到在服务器捕获的日期,因为此解决方案可能冲突月份和日期小于或等于12 – codein