我有一个DropDownlist和Webgrid。当我将所有数据加载到Bind然后我需要根据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);
}
答案 0 :(得分:1)
您应该在行动中返回PartialView
:
return PartialView(model.ReceiptList);
仅当您有一个名为SelectCustomerByID
的视图时才有效。如果您有一个具有其他名称的视图,则可以在重载中指定:
return PartialView("_NameOfPartialView", model.ReceiptList);
然后在success
的{{1}}函数中
$.post()
注意:我认为您必须从function (ReceiptList) {
// ReceiptList will contain the html of the grid.
$('#id-of-datagrid').html(ReceiptList);
}
中删除Receipt model
参数。
答案 1 :(得分: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.cshtml创建为PartialView
,并将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);
}
现在它会正常工作..