0

I have an HTTPPOST action method that receives a model and saves it to the database:

[HttpPost]
public ActionResult AddDocument(Document doc){
   DocumentRepository repo= GetDocumentRepository();
   repo.SaveDocument(doc);
   return View(viewName: "DocViewer", model: doc);
}

So this method receives the model, saves it and then returns it to the DocViewer view to display the added document. I have two problems including the one in the question

  1. If I press F5 after the DocViewer is presented I get a warning that the post method will be invoked again. How do I avoid this? I'm sure there's a general practice
  2. In the DocViewer view I have defined HTML elements like this:
<div>Full name</div>
<div>@Html.LabelFor(x=>x.FullName)</div> 
<div>Address</div>
<div>@Html.LabelFor(x=>x.Address)</div> //and so on

But what I get is the following output:

Full name FullName
Address Address

Shouldn't I get the actual value but not the property name (or the Display Name if it's provided)?

4

2 回答 2

2

In Post action do not return model object back to view:

[HttpPost]
public ActionResult AddDocument(Document doc)
{
   DocumentRepository repo= GetDocumentRepository();
   repo.SaveDocument(doc);
   //return View("DocViewer");
   TempData["Document"] = doc;
   return RedirectToAction("DocViewer","ControllerName");
}

and in DocViewer action:

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

}

UPDATED:

you have to redirect to DocViewer view via its action to avoid form post again if F5 pressed.

See details here

于 2014-06-27T06:29:18.463 回答
0

The first problem was indeed solved by Ehsan's answer. I shouldn't be returning a model object to the view, instead I should redirect to another action method. The second problem arose because of the nature of LabelFor helper method. The thing is LabelFor just creates labels, which is meant to label values. To show the actual value not using text-box there's another method called DisplayTextFor. After using that method I could get the actual value.

于 2014-06-27T07:07:35.260 回答