[英]MVC4 Ajax Request
This is my Controller class 这是我的控制器课
[HttpGet]
public ActionResult ContactUs()
{
if (Request.IsAjaxRequest())
{
return PartialView("_ContactUs");
}
return View();
}
My problem return PartialView("_ContactUs"); 我的问题返回PartialView(“ _ ContactUs”); is not getting executing in MVC4 directly return View(); 没有在MVC4中执行,直接返回View(); is getting executed 正在执行
You need to use an action method selector to differentiate between Ajax and non-Ajax requests. 您需要使用操作方法选择器来区分Ajax请求和非Ajax请求。 So, implement ActionMethodSelectorAttribute and decorate your action method with that attribute (true). 因此,实现ActionMethodSelectorAttribute并用该属性装饰您的操作方法(true)。 See sample code below. 请参见下面的示例代码。
[HttpGet]
[MyAjax(true)]
public ActionResult ContactUs()
{
if (Request.IsAjaxRequest())
{
return PartialView("_ContactUs");
}
return View();
}
//..
public class MyAjaxAttribute : ActionMethodSelectorAttribute
{
private readonly bool _ajax;
public AjaxAttribute(bool ajax)
{
_ajax = ajax;
}
// Determines whether the action method selection is valid for the specified controller context
public override bool IsValidForRequest(
ControllerContext controllerContext,
MethodInfo methodInfo)
{
return _ajax == controllerContext.HttpContext.Request.IsAjaxRequest();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.