1

我正在开发一个 MVC 3 Razor 应用程序,当我单击浏览器中的后退按钮时遇到问题。我的应用程序工作流程:

  1. 从下拉列表中选择设施
  2. WebGrid 会填充设施建筑物的列表。
  3. 单击图像以编辑建筑物
  4. 单击浏览器的后退按钮,将出现步骤 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 这可能是问题吗?选择下拉列表中的项目后,我应该使用表单帖子吗?

4

2 回答 2

3

我为 Intranet 制作应用程序时发生了类似的事情......但是,嘿,振作起来,至少你的公司使用 IE9......我不得不创造奇迹,试图让带有 JQuery 的 MVC3 Razor 应用程序与 IE7 一起工作......

好的,现在重要的是,我在 IE 的缓存中遇到了类似的问题,看来这个浏览器的缓存与普通新时代浏览器的工作方式“不同”,你可以试试这个:

按 F12 并转到选项卡Cache并检查Always refresh from server

然后检查一切是否正常,如果是,请告诉您的网络管理员为所有将使用您正在制作的新应用程序的 IE 浏览器制定新策略。

还要检查这个https://superuser.com/questions/81182/how-to-force-internet-explorer-ie-to-really-reload-the-page

希望能帮助到你!

于 2012-07-08T01:20:07.750 回答
0

您需要将 Location 添加到属性以使其与 IE9 一起使用

[OutputCache(Location = OutputCacheLocation.ServerAndClient, NoStore = true, Duration = 0, VaryByParam = "None")]
于 2013-10-07T09:37:52.703 回答