MVC 4 FormCollection未重新提交时更新

时间:2013-10-07 16:03:39

标签: asp.net-mvc forms

以下是我的问题:

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

    <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
    

    编辑:

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

1 个答案:

答案 0 :(得分: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获得了cacheability参数。

<强> ... Controller.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 ...;
        }
    }
}

所以,禁用缓存!