2

以下是我的问题的事实:

  1. 我在 ASP.Net MVC 4 Web 应用程序中有一个表单。
  2. 表单上的字段与模型中的显式静态属性无关。相反,它们是可以更改的动态字段。
  3. 提交表单时,我使用 FormcCollection 检索输入到字段值中的值。
  4. 我第一次提交表单时,一切都很好:FormCollection 的值准确地反映了表单值。
  5. 如果由于一个或多个无效字段而导致字段值出现问题,我会重新显示表单。
  6. 如果用户更改表单的值以更正验证错误,然后重新提交相同的表单,则不会更新 FormCollection 以反映最新的表单值。相反,它始终包含第一次提交的字段值。

为什么会发生这种情况,我该如何纠正?

<HttpPost()> _
<HttpParamAction()> _
Function Upload(ByVal model As MaxDocument, formcollection As FormCollection) As ActionResult
  Dim sCriteria As String = ""
  Dim nKeyIndex As Integer = 0
  Dim nFieldIndex As Integer = -1
  Dim sFieldValue As String = ""

  Try
    ' Build sCriteria from the submitted formcollection
    model.GetFileCabinetFieldList()
    For nFieldIndex = 0 To (model.IndexFieldCount - 1)
      sFieldValue = ""
      If nFieldIndex > 0 Then
        sCriteria += "~"
      End If
      Dim fcf As MaxServerLib.FileCabinetField = model.criterionAtIndex(nFieldIndex)
        ' Get the field value corresponding to this field
        For Each oKey As Object In formcollection.AllKeys
          If oKey.ToString = fcf.sFieldName Then
            sFieldValue = formcollection(oKey.ToString)
            Exit For
          End If
        Next
        sCriteria += sFieldValue
      Next
      If sCriteria = "" Then sCriteria = "[BlankIndex]"

      ' Set the criteria property of the model, which will be used for both field validation and document export.
      model.Criteria = sCriteria

      ' First thing we do is to perform valiation of the criteria
      model.ValidateFieldValues()
      If Not model.AllFieldValuesValid() Then
        ' Handle case where one or more field values are invalid.
        ' In this case we want to redisplay the form but show an error message listing the invalid fields

        model.HasAttemptedUpload = True
        ' Set tempData before redirect:
        TempData("MaxDocument") = model
        Return RedirectToAction("Index")
      Else
        ' All field values are valid, now attempt to add the document
    ...
      End If

    Catch ex As Exception
      System.Diagnostics.Debugger.Break()
    End Try
  'End If

  ' If we got this far, something failed, redisplay form
  Return View(model)

End Function

编辑:

似乎正在发生的事情是浏览器已经缓存了第一个帖子的帖子操作,并且在每个后续帖子(第一个帖子之后)它呈现第一个帖子的缓存结果,而不是呈现当前帖子的结果。为什么会这样做?

4

1 回答 1

0

我创建了一个类NoCacheControlAttribute

NoCacheControlAttribute.cs:

using System;
using System.Web;
using System.Web.Mvc;
namespace Company.Common.Web.MVC
{
    public class NoCacheControlAttribute : ActionFilterAttribute
    {
        private readonly HttpCacheability _cacheability;
        public NoCacheControlAttribute(HttpCacheability cacheability)
        {
            _cacheability = cacheability;
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            cache.SetAllowResponseInBrowserHistory(false);
            cache.SetCacheability(_cacheability);
            cache.SetExpires(DateTime.Now);
            cache.SetNoServerCaching();
            cache.SetNoStore();
        }
    }
}

我通过 Autofac Dependency Injection / Constructor Injection 获得了可缓存性参数。

...控制器.cs

using ...
using System.Web;
using System.Web.Mvc;
using Company.Common.Web.MVC;
using ...
namespace Company.Project.Web.Controllers
{
    [NoCacheControl(HttpCacheability.NoCache)]
    public class ContractController : Controller
    {
        private readonly IRepository _repository;
        public ContractController(IRepository repository)
        {
            _repository = repository;
        }
        [HttpGet]
        public ActionResult ActionTest(string id)
        {
            ...
            return ...;
        }
    }
}

因此,缓存被禁用!

于 2013-10-08T02:48:03.627 回答