问题
I call the same controller many times from _Layout.cshtml view. So in this controller, how can I discover at runtime if it's still same page that is rendering or if a brand new page request is being made?
In asp.net you can use ispostback to figure this out. How can you tell if a brand new request is being made for a page in MVC3?
Thanks
回答1:
There's no such think on MVC. You've actions that can handle POSTs, GETs or both. You can filter what each action can handle using [HttpPost]
and [HttpGet]
attributes.
On MVC, the closest you can get to IsPostBack
is something like this within your action:
public ActionResult Index()
{
if (Request.HttpMethod == "POST")
{
// Do something
}
return View();
}
Therefore,
[HttpPost]
public ActionResult Create(CreateModel model)
{
if (Request.HttpMethod == "POST") // <-- always true
{
// Do something
}
return RedirectToAction("Index");
}
回答2:
Might I also suggest you implement it as property in your controller base class like:
protected bool IsPostback
{
get { return Request.HttpMethod == "POST"; }
}
-Marc
回答3:
Actually are this way:
if (Request.Method == "POST")
{
}
来源:https://stackoverflow.com/questions/8275384/mvc3-page-ispostback-like-functionality