2016-08-23 29 views
-1

該表格當前顯示所有日子的所有數據庫數據。例如,08/06/2016,08/07/2016,08/08/2016,08/09/2016。我如何改變它只能顯示某些日子?就像用戶在jQuery UI數據閱讀器中輸入兩個日期一樣,那麼表格只顯示兩天的數據?如何過濾asp.net中的表格行mvc

我使用Ajax。我通過文本框最新的數據操作方法過濾日期,但有錯誤:

The parameters dictionary contains a null entry for parameter 'activityStartTime2' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult ShowDateTable(System.DateTime, System.DateTime)' in 'ContosoSite.Controllers.ActivitiesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

<div> 
    Start Date: <input type="datetime" name="startdate" id="start_datepicker2" /> 
    End Date: <input type="text" name="enddate" id="end_datepicker2" /> 
</div> 

<div> 
    <button id="submit_date2" class="btn btn-warning btn-lg">Submit Date</button> 
</div> 

<div id="display_table2" hidden="true" > 
    <p> 
     @Html.ActionLink("Create New", "Create") 
    </p> 
    <table class="table"> 
     <tr> 
      <th></th> 
      <th>@Html.DisplayNameFor(model => model.ActivityName)</th> 
      <th>@Html.DisplayNameFor(model => model.Latitude)</th> 
      .... 
     </tr> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td> 
        <a class="btn btn-info btn-lg" onclick="location.href='@Url.Action("Map", "Activities")'"> 
         <span class="glyphicon glyphicon-map-marker"></span> Map 
        </a> 
       </td> 
       <td>@Html.DisplayFor(modelItem => item.ActivityName)</td> 
       <td data-toggle="modal" data-target="#myModal">@Html.DisplayFor(modelItem => item.Latitude)</td> 
       .... 
      </tr> 
     } 
    </table> 
</div> 

腳本

<script> 
    $("#submit_date2").click(function(){ 
     $.ajax({ 
      url: "/Activities/ShowDateTable", 
      data : { activityStartTime2: $("#start_datepicker2").val(), 
       activityEndTime2: $("#end_datepicker2").val() 

      }, 
      success: function (response) { 
       $("#display_table2").show(); 
      } 
     }); 

     //$("#show_count_number").show(); 

     //$("#display-start-date").text($("#start_datepicker").val()); 
     //$("#display-end-date").text($("#end_datepicker").val()); 
    }) 
</script> 

控制器

public ActionResult ShowDateTable(DateTime activityStartTime2, DateTime activityEndTime2) 
{ 
    try 
    { 
     using (var dbShowDate = new ContosoUniversityDataEntities()) 
     { 

      return View(dbShowDate.Activities.Where(a => a.ActivityTime >= activityStartTime2 & a.ActivityTime <= activityEndTime2).ToList()); 
     } 
    } 
    catch (Exception ex) 
    { 
     return Content("this not correct"); 
    } 
+0

你需要,無論是通過重定向到一個新的頁面或使用AJAX更新現有網頁的日期選擇器的值發送到服務器並重新生成基於過濾器的視圖。 –

+0

嗨斯蒂芬,是的,我使用ajax,請查看我的代碼。如果你能提供一些反饋,它會很棒。非常感謝 – love1point

+0

我在頂級代碼中顯示輸入文本代碼 – love1point

回答

0

如果您activitystarttime2和activityendtime在日期時間字符串和ActivityTime。 比

return View(dbShowDate.Activities.Where(a => a.ActivityTime >= DateTime.Parse(activityStartTime2) && a.ActivityTime <= DateTime.Parse (activityEndTime2)).ToList()); 

如果雙方在日期時間不是簡單的在你的查詢添加&。

return View(dbShowDate.Activities.Where(a => a.ActivityTime >= activityStartTime2 && a.ActivityTime <= activityEndTime2).ToList()); 
+0

這與這個問題有什麼關係(這是關於拋出的異常) –

+0

它datetime可空類型比使用a => a.ActivityTime.Value – DBB

+0

我已經有了像你這裏說的代碼 – love1point