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"
You can check the Request. HttpMethod property. Save this answer.
A partial view is sent via AJAX and must be consumed by JavaScript on the client side, while a View is a full postback. That's it. Normally this means that a View is more of a complete web page and a partial view is reserved for individual sections or controls.
Both GET and POST method is used to transfer data from client to server in HTTP protocol but the Main difference between the POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from ...
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.
<% if (System.Web.HttpContext.Current.Request.HttpMethod.ToString() == "GET") { %><!-- This is GET --><% } else if (System.Web.HttpContext.Current.Request.HttpMethod.ToString() == "POST") { %><!--This is POST--><%} else { %><!--Something another --><% } %
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With