My idea is to pass a string data to a strongly-typed view as follows:
Controller:
public ActionResult Confirmation()
{
string message = TempData["message"] as string;
if (message != null)
return View(message);//it does not work
else
return RedirectToAction("Index");
}
View:
@Model System.String
@{
ViewBag.Title = "Confirmation";
}
<h2>
Confirmation</h2>
@Model
However, it does not work.
How to make it work?
EDIT 1
I can make it work by downcast message to object as follows:
return View((object)message);
I think MVC is getting slightly confused here. It won’t work because it will try to return a ViewName of whatever you have in Message at the time.
There are at least three overloads you can use:
MVC in your case is trying to do the first one but is using your variable Message as the view name. Try changing it to this to force it to use the correct overload:
.. and see what happens then.
Edit: Didn’t realise you were not using the Index action; updated the example, but the point remains the same.