gpt4 book ai didi

c# - 如何在 ASP.net MVC 中使用分部 View 在 Webgrid 中绑定(bind)数据

转载 作者:行者123 更新时间:2023-11-30 17:50:02 25 4
gpt4 key购买 nike

我是 .Net MVC 的新手。我需要使用 PartialView() 将数据绑定(bind)到 webgrid 返回,或者如果可以使用 JSON 返回,现在我的代码将在 Dropdown 更改时执行,但它不会绑定(bind) web 网格中的值。

JavaScript、模型、 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) {
alert(firstDDLValue);
$('#divgrid').html(ReceiptLists);
});
});

});
</script>

查看:

<div id="divgrid" style="margin-top: 15px;">
@{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);}
<div id="gridContent">
@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
columns: grid.Columns(
grid.Column("Id", "ID", style: "id"),
grid.Column("Cust_Name", "Cust Name", style: "PName"),
grid.Column("Pay_Amount", "Pay_Amount", style: "ICode"),
grid.Column("Pay_Mode", "Pay_Mode", style: "IName"),
grid.Column("Bank_Name", "Bank_Name", style: "Weight"),
grid.Column("Bank_Address", " Bank_Address", style: "MakingCharge"),
grid.Column("ChequeNo", "ChequeNo", style: "Certification"),
grid.Column("Cheque_Date", " Cheque_Date", style: "Price"),
grid.Column("Date", "Date", style: "Price"),
grid.Column("Edit", "", @<a href='../Admin/EditReceipt?id=@item.ID' ><img src="~/Images/edit.png" alt='Edit' /></a>, style: "width:10%"),
grid.Column(header: "Delete", format: @<text><a href="@Url.Action("DeleteReceipt", "Admin", new { Id = item.ID })" onclick="javascript:return confirm('Are you sure you'd like to delete this product?');"><img src="../Images/delete.png" alt='Delete' /></a></text>) ))
@if (grid.HasSelection)
{
Receipt = (JewellaryWeb.Models.Receipt)grid.Rows[grid.SelectedIndex].Value;
<b>Id</b> @Receipt.Id<br />
<b>Name</b> @Receipt.Cust_Name<br />
<b>Amount</b> @Receipt.Pay_Amount<br />
<b>PayMode</b> @Receipt.Pay_Mode<br />
<b>BankName</b> @Receipt.Bank_Name<br />
<b>BankAddress</b> @Receipt.Bank_Address<br />
<b>ChequeNumber</b> @Receipt.ChequeNo<br />
<b>ChequeDate</b> @Receipt.Cheque_Date<br />
}
</div>

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 PartialView(model.ReceiptList);
}

最佳答案

我找到了答案我只是修改了我的脚本并使用 ParitalView 在 gird 中绑定(bind)数据我的解决方案代码是:

脚本

<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);
}

现在它可以正常工作了..

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com