2011-02-14 12 views
18

MVC, aynı görünümleri http get veya post için eşlemek için eylem özelliklerini kullanır:ASP.NET MVC'de görünümün GET veya POST için olup olmadığı nasıl belirlenir?

 [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(); 
} 

MVC görünümünde, görünümün http veya http post için olup olmadığını nasıl belirleyebilirim?


Görünümleri IsPost

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

cevap

29

System.Web.HttpContext.Current.Request.HttpMethod mağaza geçerli yöntemdir. Veya sadece Request.HttpMethod görünümünün içinde, ancak bunu kontrol etmeniz gerekiyorsa, yaklaşımınızda bir sorun olabilir.

Yeniden göndermeyi oluşturmak için Yönlendirme Sonrası-Alma modelini kullanmayı düşünün.

9
<% 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 --><% } % 
İlgili konular