我正在开发一个 MVC 3 Razor 应用程序,当我单击浏览器中的后退按钮时遇到问题。我的应用程序工作流程:
- 从下拉列表中选择设施
- WebGrid 会填充设施建筑物的列表。
- 单击图像以编辑建筑物
- 单击浏览器的后退按钮,将出现步骤 1 中的下拉列表,但没有 CSS 格式。如果我单击 F5 进行刷新,那么一切都会重置并且 CSS 格式会恢复。
我正在使用 VS2010 和以 IE9 作为默认浏览器的 ASP.NET 开发服务器。我已将 OutputCache 属性添加到控制器中的每个 ActionResult。
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
这是我在从 PartialView 构建的 WebGrid 中的链接
grid.Column(header: "",
format: @<text><a href="@Url.Action("Edit", "BuildingModels", new { @id = item.FACInventoriesId })"><img src="@Url.Content("~/Content/images/edit.png")"
alt='Edit' title='Edit' border='0'/></a></text>)
当从编辑建筑物(第 4 步)中单击后退按钮时,如何让浏览器显示 WebGrid(第 2 步)?还有什么想法为什么我单击后退按钮时缺少 CSS 格式?
这是控制器代码:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ViewResult Index()
{
ViewBag.Systems = buildingsVM.GetSystemsList();
return View();
}
[HttpPost]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult GetFacilityDetails(int systemId, string facilityId)
{
try
{
ViewBag.Systems = buildingsVM.GetSystemsList();
var facility = buildingsVM.GetFacilityDetails(systemId, facilityId);
facility.Buildings = buildingsVM.GetFacilityBuildings(systemId, facilityId);
var bldgsHtml = ViewsUtility.RenderPartialViewToString(this, "_Buildings", facility.Buildings);
TempData["CurrentFacility"] = facility;
return Json(new { ok = true, facdata = facility, bldgs = bldgsHtml, message = "ok" });
}
catch (Exception ex)
{
return Json(new { ok = false, message = ex.Message });
}
}
[HttpPost]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult GetSystemFacilities(int systemId)
{
try
{
var facilities = buildingsVM.GetFacilitesBySystemId(systemId);
return Json(new { ok = true, data = facilities, message = "ok" });
}
catch (Exception ex)
{
return Json(new { ok = false, message = ex.Message });
}
}
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult Edit(int id)
{
var facility = TempData["CurrentFacility"] as FacilityModel;
return View(buildingsVM.GetBuilding(id));
}
部分视图的代码:
@model IEnumerable<COPSPlanningWeb.Models.BuildingModel>
<!-- Current Buildings from partial view -->
@{
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: "webgridDisplay",
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", style: "webgridDisplayCenter"),
grid.Column("ConstructionDate", header: "Construction Date", format: @<text>@item.ConstructionDate.ToString("MM/dd/yyyy")</text>),
grid.Column("ExtSquareFeet", header: "Exterior Sq. Ft.", format: (item) => string.Format("{0:n0}", item.ExtSquareFeet)),
grid.Column("IntSquareFeet", header: "Interior Sq. Ft.", format: (item) => string.Format("{0:n0}", item.IntSquareFeet)),
grid.Column("IU_Avail", header: "IU Available"),
grid.Column("SpaceAvail", header: "Space Available"),
grid.Column("FixedAssetValue", header: "Fixed Asset Value", format: (item) => string.Format("{0:C}", item.FixedAssetValue)),
grid.Column("FixedEquipValue", header: "Fixed Equipment Value", format: (item) => string.Format("{0:C}", item.FixedEquipValue)),
grid.Column(header: "",
format: @<text><a href="@Url.Action("Edit", "BuildingModels", new { @id = item.FACInventoriesId })"><img src="@Url.Content("~/Content/images/edit.png")"
alt='Edit' title='Edit' border='0'/></a></text>),
grid.Column(header: "",
format: @<text><a href="@Url.Action("Delete", "BuildingModels", new { @id = item.FACInventoriesId })"><img src="@Url.Content("~/Content/images/trash.png")"
alt='Delete' title='Delete' border='0'/></a></text>)
))
}
}
编辑视图中的代码:
@model COPSPlanningWeb.Models.BuildingModel
@{
ViewBag.Title = "Add/Edit Inventory";
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<table style="width: 100%;" class="display">
@Html.HiddenFor(model => model.FacilityId)
@Html.HiddenFor(model => model.FACInventoriesId)
<tr>
<th colspan="2" style="text-align: left;">
Building Information - Edit Inventory
</th>
</tr>
<tr>
<td class="fieldlabel">
Facility Name
</td>
<td class="fielddata">
</td>
</tr>
<tr>
<td class="fieldlabel">
Building Number
</td>
<td class="fielddata">
@Html.EditorFor(model => model.BuildingNumber)
@Html.ValidationMessageFor(model => model.BuildingNumber)
</td>
</tr>
<tr>
<td class="fieldlabel">
Construction Date
</td>
<td class="fielddata">
@Html.EditorFor(model => model.ConstructionDate, "DateTime")
@Html.ValidationMessageFor(model => model.ConstructionDate)
</td>
</tr>
<tr>
<td class="fieldlabel">
Exterior Sq. Ft.
</td>
<td class="fielddata">
@Html.EditorFor(model => model.ExtSquareFeet)
@Html.ValidationMessageFor(model => model.ExtSquareFeet)
</td>
</tr>
<tr>
<td class="fieldlabel">
Interior Sq. Ft.
</td>
<td class="fielddata">
@Html.EditorFor(model => model.IntSquareFeet)
@Html.ValidationMessageFor(model => model.IntSquareFeet)
</td>
</tr>
<tr>
<td class="fieldlabel">
IU Available
</td>
<td class="fielddata">
@Html.EditorFor(model => model.IU_Avail)
@Html.ValidationMessageFor(model => model.IU_Avail)
</td>
</tr>
<tr>
<td class="fieldlabel">
Space Available
</td>
<td class="fielddata">
@Html.EditorFor(model => model.SpaceAvail)
@Html.ValidationMessageFor(model => model.SpaceAvail)
</td>
</tr>
<tr>
<td class="fieldlabel">
Fixed Asset Value
</td>
<td class="fielddata">
@Html.EditorFor(model => model.FixedAssetValue)
@Html.ValidationMessageFor(model => model.FixedAssetValue)
</td>
</tr>
<tr>
<td class="fieldlabel">
Fixed Equipment Value
</td>
<td class="fielddata">
@Html.EditorFor(model => model.FixedEquipValue)
@Html.ValidationMessageFor(model => model.FixedEquipValue)
</td>
</tr>
<tr>
<td class="fieldlabel">
Total Fixed Asset Value
</td>
<td class="fielddata">
@Html.EditorFor(model => model.TotalFixedAssetValue)
@Html.ValidationMessageFor(model => model.TotalFixedAssetValue)
</td>
</tr>
<tr>
<td class="fieldlabel">
Total Fixed Equipment Value
</td>
<td class="fielddata">
@Html.EditorFor(model => model.TotalFixedEquipValue)
@Html.ValidationMessageFor(model => model.TotalFixedEquipValue)
</td>
</tr>
<tr>
<td colspan="2">
<table class="display" style="text-align: center;">
<tr>
<td>
@Html.ActionLink("Add/Edit Spaces", "Index")
</td>
<td>
<input type="submit" value="Save Changes" class="button" />
</td>
<td>
@Html.ActionLink("Back to Buildings List", "Index")
</td>
</tr>
</table>
</td>
</tr>
</table>
}
当我从编辑视图单击后退按钮时,我希望再次看到 WebGrid(构建列表),但我得到的第一个视图没有任何 CSS 格式。
感谢 Luis,我能够解决 CSS 格式问题,但是当我单击后退按钮时,我仍然看不到 WebGrid。我正在使用 JSON 来填充 WebGrid 这可能是问题吗?选择下拉列表中的项目后,我应该使用表单帖子吗?