c# - 如何使用 PartialView 在 ASP.Net MVC 的 webgrid 中绑定(bind)数据

标签 c# javascript asp.net-mvc json asp.net-mvc-4

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

我的代码是:

脚本:

它将调用 Controller 函数

<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 代码:

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 的 View 时有效。如果您有一个具有其他名称的 View ,您可以在重载中指定它:

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

然后在 $.post()success 函数中:

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

注意:我认为您必须从 SelectCustomerByID 中删除 Receipt model 参数。

关于c# - 如何使用 PartialView 在 ASP.Net MVC 的 webgrid 中绑定(bind)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19197476/

相关文章:

javascript - React 未捕获类型错误 : Cannot read property 'replace' of undefined

c# - TypeInitializationException 发生在 'System.Data.Entity.dll' - 类型初始值设定项 'ExtentPlaceholderCreator'

C# 匿名类和列表<object>

c# - 将 wsdl Fedex 文件添加到 c# .Net

c# - 维基百科 A* 寻路算法需要大量时间

c# - 解码这种路径url?

javascript - 如何在新窗口中从 slider 打开图像

javascript - 如何在 Meteorjs 的特定 URL 路径中隐藏模板上的按钮?

c# - ASP.NET 5 API 向调用方返回异常

javascript - MVC 4.0 的当前上下文中不存在名称 '<name>'