如何确定如果视图是在ASP.NET MVC GET或POST? [英] How to determine if the view is for GET or POST in ASP.NET MVC?

查看:175
本文介绍了如何确定如果视图是在ASP.NET MVC GET或POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC使用action属性映射为相同的看法HTTP GET或POST:

  [HTTPGET]
 公众的ActionResult指数()
 {
    ViewBag.Message =消息;
    返回查看();
 } [HttpPost]
 公众的ActionResult指数(十进制,十进制B,字符串操作)
 {
     ViewBag.Message =计算结果;
     ViewBag.Result = Calculation.Execute(A,B,操作);
     返回查看();
 }

在MVC视图,我怎么能确定视图是HTTP GET或HTTP POST?


在浏览它 IsPost

  @ {
     VAR消息=;
     如果(IsPost)
      {
            消息=这是回发;
      }
       其他
    {
            消息=这是一个没有回发;
    }
}


解决方案

System.Web.HttpContext.Current.Request.HttpMethod 商店当前方法。或只是 Request.HttpMethod 的视图中,但如果你需要检查这一点,有可能是坏了你的方法。

考虑使用后重定向-获取模式,形成重新发布。

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";
    }
}

解决方案

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.

这篇关于如何确定如果视图是在ASP.NET MVC GET或POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆