我有一个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);
}

最佳答案

您应该在操作中返回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 model中删除​​SelectCustomerByID参数。

10-07 19:52
查看更多