asp.net-mvc - mvc - url action post




How to determine if the view is for GET or POST in ASP.NET MVC? (2)

System.Web.HttpContext.Current.Request.HttpMethod stores current method. Or just Request.HttpMethod inside of view, but if you need to check this, there may be something wrong with your approach.

Think about using Post-Redirect-Get pattern to form reposting.

MVC use action attributes to map the same view for http get or post:

 [HttpGet] 
 public ActionResult Index()
 {
    ViewBag.Message = "Message";
    return View();
 }

 [HttpPost]
 public ActionResult Index(decimal a, decimal b, string operation)
 {
     ViewBag.Message = "Calculation Result:";
     ViewBag.Result = Calculation.Execute(a, b, operation);
     return View();
 }

In the MVC view, how can I determine if the view is for http get or http post?


in Views it is IsPost

@{
     var Message="";
     if(IsPost)
      {
            Message ="This is from the postback";
      }
       else
    {
            Message="This is without postback";
    }
}

PS: For dot net core it is:

Context.Request.Method == "POST"

For dot net core it is:

Context.Request.Method == "POST"





asp.net-mvc