2012-07-20 5 views
0

이 내 컨트롤러 클래스MVC4 Ajax 요청

[HttpGet] 
    public ActionResult ContactUs() 
    { 
     if (Request.IsAjaxRequest()) 
     { 
      return PartialView("_ContactUs"); 
     } 

     return View(); 
    } 

내 문제 리턴 PartialView ("_ CONTACTUS")입니다; MVC4에서 직접 실행되지 않습니다. View()를 반환합니다. 이 실행 중입니다.

+0

이 작업을 호출하는 데 사용하는 자바 스크립트 코드는 무엇입니까? – stevethethread

답변

1

Ajax 요청과 비 Ajax 요청을 구분하기 위해 조치 방법 선택 도구를 사용해야합니다. 따라서 ActionMethodSelectorAttribute를 구현하고 해당 속성을 사용하여 작업 메서드를 꾸미십시오 (true). 아래 샘플 코드를 참조하십시오.

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