2017-09-14 91 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