Asp.net mvc 4 HttpPost操作方法在重新加载其显示的视图时被调用

Asp.net mvc 4 HttpPost操作方法在重新加载其显示的视图时被调用,asp.net-mvc-4,viewmodel,actionmethod,Asp.net Mvc 4,Viewmodel,Actionmethod,我有一个HTTPPOST操作方法,该方法接收模型并将其保存到数据库: [HttpPost] public ActionResult AddDocument(Document doc){ DocumentRepository repo= GetDocumentRepository(); repo.SaveDocument(doc); return View(viewName: "DocViewer", model: doc); } 因此,此方法接收模型,保存它,然后将其返回到D

我有一个HTTPPOST操作方法,该方法接收模型并将其保存到数据库:

[HttpPost]
public ActionResult AddDocument(Document doc){
   DocumentRepository repo= GetDocumentRepository();
   repo.SaveDocument(doc);
   return View(viewName: "DocViewer", model: doc);
}
因此,此方法接收模型,保存它,然后将其返回到
DocViewer
视图以显示添加的文档。我有两个问题,包括问题中的一个

  • 如果在出现
    DocViewer
    后按F5,则会收到一条警告,提示将再次调用post方法。我如何避免这种情况?我相信有一个普遍的做法
  • DocViewer
    视图中,我定义了如下HTML元素:

  • 我是否应该获取实际值,而不是属性名称(或显示名称,如果提供的话)?

    在后期操作中,不要将模型对象返回到视图:

    [HttpPost]
    public ActionResult AddDocument(Document doc)
    {
       DocumentRepository repo= GetDocumentRepository();
       repo.SaveDocument(doc);
       //return View("DocViewer");
       TempData["Document"] = doc;
       return RedirectToAction("DocViewer","ControllerName");
    }
    
    并在DocViewer中执行以下操作:

    public ActionResult DocViewer()
    {
       Document doc = TempData["DocViewer"] as Document;
       return View(doc);
    
    }
    
    更新:

    您必须通过其操作重定向到DocViewer视图,以避免在按下F5时再次发布表单


    参见

    第一个问题确实通过Ehsan的回答解决了。我不应该将模型对象返回到视图,而是应该重定向到另一个操作方法。
    第二个问题是由于labelforhelper方法的性质引起的。事情是
    LabelFor
    只创建标签,这意味着标签值。要显示不使用文本框的实际值,还有另一种方法称为
    DisplayTextFor
    。使用该方法后,我可以得到实际值。

    通过它的操作将视图重定向到视图,而不是返回视图。这确实解决了第一个问题。第二个怎么样?为什么我不获取模型属性值而不是它们的名称呢?现在都设置好了,@Ehsan。我找到了原因。如果你介意的话,请看一下我的答案。第二个问题是什么?我没有理解@MikeJMWhen使用@Html.labelor@MikeJMWhen。因为我没有得到模型属性的值,所以我得到了属性名称。例如,对于属性FullName,我没有得到Joe Black,我得到的是FullName。
    [HttpPost]
    public ActionResult AddDocument(Document doc)
    {
       DocumentRepository repo= GetDocumentRepository();
       repo.SaveDocument(doc);
       //return View("DocViewer");
       TempData["Document"] = doc;
       return RedirectToAction("DocViewer","ControllerName");
    }
    
    public ActionResult DocViewer()
    {
       Document doc = TempData["DocViewer"] as Document;
       return View(doc);
    
    }