1

我有一个 DropDownlist 和 Webgrid。当我加载页面所有绑定的数据时,我需要根据 DropDownlist 的变化在 Webgrid 中绑定数据。我该怎么做。

我的代码是:

Script:

它将调用控制器函数

<script type="text/javascript">
    $(document).ready(function () {
         $("#Cust_Id").change(function () {
          firstDDLValue = $("#Cust_Id").val();    
          $.post(
            '@Url.Action("SelectCustomerByID", "Admin")', 
            { 
                secondValue: firstDDLValue 
            }, 
            function (ReceiptList) {
           });
        });
    });
</script> 

Controller Code:

public ActionResult SelectCustomerByID(Receipt model,string secondValue)
{
    int CustID = 0;
    if (secondValue != "")
    {
        CustID = Convert.ToInt32(secondValue);
    }
    ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
    Receipt Receipt = new Models.Receipt();
    model.ReceiptList = Receipt.GetReceiptListByCustID(CustID);
    return View(model.ReceiptList);
}
4

2 回答 2

1

您应该在您的操作中返回 a PartialView

return PartialView(model.ReceiptList);

这仅在您有一个名为SelectCustomerByID. 如果您有另一个名称的视图,则可以在重载中指定:

return PartialView("_NameOfPartialView", model.ReceiptList);

然后在您的success功能中$.post()

function (ReceiptList) {
    // ReceiptList will contain the html of the grid.
    $('#id-of-datagrid').html(ReceiptList);
}

注意:我认为您必须Receipt modelSelectCustomerByID.

于 2013-10-05T11:57:06.527 回答
1

我找到了答案我刚刚修改了我的Script并用于ParitalView在网格中绑定数据我的解决方案代码是:

Script

<script type="text/javascript">
 $(document).ready(function () {
 $("#Cust_Id").change(function () {
 firstDDLValue = $("#Cust_Id").val();
 $.get('@Url.Action("SelectCustomerByID", "Admin")', { secondValue: firstDDLValue }, function (ReceiptList) {
  $('#gridContent').html(ReceiptList);
    });
  });
 });
</script>

我创建新的 _Recepitgrid.cshtmlPartialView并将 Webgrid 的代码编写为与主视图中的相同。

Controller

public ActionResult SelectCustomerByID(Receipt model, string secondValue)
{
 int CustID = 0;
 if (secondValue != "")
 CustID = Convert.ToInt32(secondValue);
 ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
 Receipt Receipt = new Models.Receipt();
 ReceiptList = Receipt.GetReceiptListByCustID(CustID);
 return PartialView("_Recepitgrid", ReceiptList);
}

现在它会正常工作..

于 2013-10-08T05:18:47.370 回答