MVC3 Page - IsPostback like functionality

半腔热情 提交于 2020-01-12 14:30:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!