gpt4 book ai didi

c# - 绑定(bind) WebGrid 表单 AJAX

转载 作者:行者123 更新时间:2023-11-30 14:14:44 28 4
gpt4 key购买 nike

我是 MVC3 和 Razor 的新手,一旦从 AJAX post 返回数据,我需要有关绑定(bind)/加载 WebGrid 的帮助。任何帮助将不胜感激(项目到期日即将来临);)

我的场景是这样的:我有两个级联下拉列表。第一个列表包含数据库中的区域。选择区域后,它会在第二个下拉列表中显示设施列表。选择设施后,我需要使用建筑物列表填充 WebGrid。我的级联下拉菜单正常工作

索引.cshtml:

@using ThisController = MyProject.Controllers.BuildingModelsController
@model IEnumerable<MyProject.Models.BuildingModel>

<div id="tabs-2">
<!-- Current Buildings -->
@{
if (Model != null && Model.Count() > 0)
{
var grid = new WebGrid(source: Model, rowsPerPage: ThisController.PageSize, ajaxUpdateContainerId: "tabs-2", defaultSort: "BuildingNumber");
grid.Bind(Model, rowCount: Model.Count(), autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);

grid.GetHtml(
tableStyle: "display",
alternatingRowStyle: "alt",
columns: grid.Columns(
//grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { EmployeeID = item.EmployeeID, ContactID = item.ContactID })),
grid.Column("BuildingNumber", header: "Building Number"),
grid.Column("ConstructionDate", header: "Construction Date"),
grid.Column("ExtSquareFeet", header: "Exterior Sq. Ft."),
grid.Column("IntSquareFeet", header: "Interior Sq. Ft."),
grid.Column("IU_Avail", header: "IU Available"),
grid.Column("SpaceAvail", header: "Space Available"),
grid.Column("FixedAssetValue", header: "Fixed Asset Value"),
grid.Column("FixedEquipValue", header: "Fixed Equipment Value")
));
}
else
{
@:There are no buildings at this facility.
}
}
</div>

这是我的 AJAX 调用

var regId = $("#ddlRegion").val();
var facId = $("#ddlFacility").val();

$.ajax({
type: "POST",
url: '@Url.Action("GetFacilityDetails")',
data: { regionId: regId, facilityId: facId },
success: function (returndata) {
if (returndata.ok) {
var itemData = returndata.data;
var address = itemData.Address + " " + itemData.City + " " + itemData.State + " " + itemData.Zip;

$("#lblFacilityType").html(itemData.FacilityType);
$("#lblFacilityPurpose").html(itemData.FacilityPurpose);
$("#lblFacilityStatus").html(itemData.FacilityStatus);
$("#lblFacilityAddress").html(address);

$("#tabs").tabs({ disabled: [] });
//need to populate webgrid here
}
else {
window.alert(' error : ' + returndata.message);
}

}
}
);

我的 Controller :

[HttpPost]
public ActionResult GetFacilityDetails(int regionId, string facilityId)
{
try
{
//ViewBag.Buildings = buildingsVM.GetFacilityBuildings(regionId, facilityId);
var facility = buildingsVM.GetFacilityDetails(regionId, facilityId);
facility.Buildings = buildingsVM.GetFacilityBuildings(regionId, facilityId) as List<BuildingModel>;

return Json(new { ok = true, data = facility, message = "ok" });
}
catch (Exception ex)
{
return Json(new { ok = false, message = ex.Message });
}
}

@Darin 我向您提出了建议的更改,但屏幕上没有显示任何内容。我也没有收到任何错误。我单步执行代码并确认 View 中的模型对象有 12 个自定义“构建模型”对象。

这是我的局部 View :

@model IEnumerable<COPSPlanningWeb.Models.BuildingModel>
@{
if (Model != null && Model.Count() > 0)
{
var grid = new WebGrid(rowsPerPage: 50, defaultSort: "BuildingNumber", ajaxUpdateContainerId: "tabs-2");
grid.Bind(Model, rowCount: Model.Count(), autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);

grid.GetHtml(
tableStyle: "display",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("BuildingNumber"),
grid.Column("ConstructionDate"),
grid.Column("ExtSquareFeet"),
grid.Column("IntSquareFeet"),
grid.Column("IU_Avail"),
grid.Column("SpaceAvail"),
grid.Column("FixedAssetValue"),
grid.Column("FixedEquipValue")
));
}
else
{
@:There are no buildings at this facility.
}
}

有趣的是,当我在浏览器中查看源代码时,我看到“此设施中没有建筑物。”,但它没有显示在屏幕上,而且当我逐步执行代码时,模型确实有我的自定义对象在调试器中。

最佳答案

您可以将 WebGrid 放入部分:

@using ThisController = MyProject.Controllers.BuildingModelsController
@model IEnumerable<MyProject.Models.BuildingModel>

<div id="tabs-2">
@Html.Partial("_Buildings")
</div>

_Buildings.cshtml 中:

<!-- Current Buildings -->
@{
if (Model != null && Model.Count() > 0)
{
var grid = new WebGrid(source: Model, rowsPerPage: ThisController.PageSize, ajaxUpdateContainerId: "tabs-2", defaultSort: "BuildingNumber");
grid.Bind(Model, rowCount: Model.Count(), autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);

grid.GetHtml(
tableStyle: "display",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("BuildingNumber", header: "Building Number"),
grid.Column("ConstructionDate", header: "Construction Date"),
grid.Column("ExtSquareFeet", header: "Exterior Sq. Ft."),
grid.Column("IntSquareFeet", header: "Interior Sq. Ft."),
grid.Column("IU_Avail", header: "IU Available"),
grid.Column("SpaceAvail", header: "Space Available"),
grid.Column("FixedAssetValue", header: "Fixed Asset Value"),
grid.Column("FixedEquipValue", header: "Fixed Equipment Value")
)
);
}
else
{
@:There are no buildings at this facility.
}
}

现在在您的 Controller 操作中,如果成功则返回此部分:

[HttpPost]
public ActionResult GetFacilityDetails(int regionId, string facilityId)
{
try
{
var facility = buildingsVM.GetFacilityDetails(regionId, facilityId);
facility.Buildings = buildingsVM.GetFacilityBuildings(regionId, facilityId) as List<BuildingModel>;
return PartialView("_Buildings", facility.Buildings);
}
catch (Exception ex)
{
return Json(new { ok = false, message = ex.Message });
}
}

并且在您的 AJAX 调用中只需刷新:

var regId = $("#ddlRegion").val();
var facId = $("#ddlFacility").val();

$.ajax({
type: "POST",
url: '@Url.Action("GetFacilityDetails")',
data: { regionId: regId, facilityId: facId },
success: function (returndata) {
if (!returndata.ok) {
window.alert(' error : ' + returndata.message);
} else {
$('#tabs').tabs({ disabled: [] });
$('#tabs-2').html(returndata);
}
}
});

关于c# - 绑定(bind) WebGrid 表单 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11351853/

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