2013-04-26 56 views
0

很抱歉,如果我的问题听起来不错,但我是一个真正的JavaScript初学者。我有2个参数“startdate”和“enddate”。我想通过它通过这个JavaScript来控制器:MVC通过日期范围控制器使用javascript

查看:(更新)

<div class="inventoryTypeList"> 
<p> 
    <label for="start">From:</label> 
     @(Html.Kendo().DatePicker() 
       .Name("from") 
       .Events(e => e.Change("startChange")) 
     ) 

     <label for="end">To:</label> 
     @(Html.Kendo().DatePicker() 
       .Name("to") 
       .Events(e => e.Change("endChange")) 
     ) 
</p> 
    <button class="k-button" id="showGrid">Search</button> 
</div> 

<script> 

    function startChange() { 
     var endPicker = $("#to").data("kendoDatePicker"), 
      startDate = this.value(); 

     if (startDate) { 
      startDate = new Date(startDate); 
      startDate.setDate(startDate.getDate() + 1); 
      endPicker.min(startDate); 
     } 
    } 

    function endChange() { 
     var startPicker = $("#from").data("kendoDatePicker"), 
      endDate = this.value(); 

     if (endDate) { 
      endDate = new Date(endDate); 
      endDate.setDate(endDate.getDate() - 1); 
      startPicker.max(endDate); 
     } 
    } 

    $(function() { 

     $("#showGrid").click(function() { 
      $.ajax({ 
       url: '@Url.Action("PastTransactionsPartial", "InventoryTransactions")', 
       type: 'POST', 
       contentType: 'application/json; charset=utf-8', 
       cache: false, 
       data: JSON.stringify({ 
        dateFrom: $('#from').val(), 
        dateTo: $('#to').val(), 
       }), 
       success: function (result) { 
        $('#divPastTransactions').replaceWith(result); 
       }, 
       error: function() { alert("Please specify from and to dates"); } 
      }); 
     }); 
    }); 

</script> 

<div id="divPastTransactions" ></div> 

控制器:(更新)

public ActionResult PastTransactionsPartial(DateTime dateFrom, DateTime dateTo) 
    { 
     var inventorytransactions = db.InventoryTransactions.Include(i => i.InventoryTransactionType).Include(i => i.InventoryType).Include(i => i.Supplier).Where(i => i.InventoryTransactionDate >= dateFrom && i.InventoryTransactionDate <= dateTo); 
     return PartialView("_PastTransactionsPartial", inventorytransactions.ToList()); 
    } 

我该怎么办呢?我在控制器中捕获了什么?

结果将显示在局部视图搜索功能

编辑下面: 关闭此线程,并把问题变成新的线程

+0

您将需要使用AJAX和POST操作方法传递它们。 – 2013-04-26 16:46:05

+0

你说过:“我有2个参数”。他们在哪? – 2013-04-26 16:47:17

+0

@DavidTansey它是开始日期和结束日期 – blurryMVC 2013-04-26 16:49:11

回答

1

查看:

<div class="searchPastTransactions"> 
<p> 
    <label for="start">From:</label> 
     @(Html.Kendo().DatePicker() 
       .Name("from") 
       .Events(e => e.Change("startChange")) 
     ) 

     <label for="end">To:</label> 
     @(Html.Kendo().DatePicker() 
       .Name("to") 
       .Events(e => e.Change("endChange")) 
     ) 
</p> 
    <button class="k-button" id="showGrid">Search</button> 
</div> 

<script> 

    function startChange() { 
     var endPicker = $("#to").data("kendoDatePicker"), 
      startDate = this.value(); 

     if (startDate) { 
      startDate = new Date(startDate); 
      startDate.setDate(startDate.getDate() + 1); 
      endPicker.min(startDate); 
     } 
    } 

    function endChange() { 
     var startPicker = $("#from").data("kendoDatePicker"), 
      endDate = this.value(); 

     if (endDate) { 
      endDate = new Date(endDate); 
      endDate.setDate(endDate.getDate() - 1); 
      startPicker.max(endDate); 
     } 
    } 

    $(function() { 

     $("#showGrid").click(function() { 
      $.ajax({ 
       url: '@Url.Action("PastTransactionsPartial", "InventoryTransactions")', 
       type: 'POST', 
       contentType: 'application/json; charset=utf-8', 
       cache: false, 
       data: JSON.stringify({ 
        dateFrom: $('#from').val(), 
        dateTo: $('#to').val(), 
       }), 
       success: function (result) { 
        $('#divPastTransactions').replaceWith(result); 
       }, 
       error: function() { alert("Please specify from and to dates"); } 
      }); 
     }); 
    }); 

</script> 

<div id="divPastTransactions" ></div> 

控制器:

public ActionResult PastTransactionsPartial(DateTime dateFrom, DateTime dateTo) 
     { 
      var inventorytransactions = db.InventoryTransactions.Include(i => i.InventoryTransactionType).Include(i => i.InventoryType).Include(i => i.Supplier).Where(i => i.InventoryTransactionDate >= dateFrom && i.InventoryTransactionDate <= dateTo); 
      return PartialView("_PastTransactionsPartial", inventorytransactions.ToList()); 
     }