2012-08-24 3 views
3

이상한 문제가 있습니다. 내보기 :Asp.Net mvc 중첩 동작 HTTPPOST

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 
@using(Html.BeginForm()) 
{ 
    <input type="submit" value="asds"/> 
} 
@Html.Action("Index2") 

내 컨트롤러 :

public class DefaultController : Controller 
{ 
    // 
    // GET: /Default1/ 

    [HttpPost] 
    public ActionResult Index(string t) 
    { 
     return View(); 
    } 


    public ActionResult Index() 
    { 
     return View(); 
    } 

    // 
    // GET: /Default1/ 

    [HttpPost] 

    public ActionResult Index2(string t) 
    { 
     return PartialView("Index"); 
    } 

      [ChildActionOnly()] 
    public ActionResult Index2() 
    { 
     return PartialView(); 
    } 
} 

내가 버튼 [HttpPost]Index(string t) 클릭

이 실행되고, 느릅 나무는 괜찮습니다. 그러나 [HttpPost]Index2(string t)이 excuted 된 후 Index에 대한 데이터를 게시하지 않았기 때문에 Index2에 대한 작업이 나에게 정말 이상합니다. 내 논리는 HttpPost 대신 [ChildActionOnly()]ActionResult Index2()을 말해 줍니 다.

왜 이런 일이 발생합니까? [HttpPost]Index2 작업의 이름을 바꾸지 않고 어떻게이 동작을 재정의 할 수 있습니까?

답변

2

기본 동작입니다. 의도적으로 설계된 것입니다. 다음

public class PreferGetChildActionForPostAttribute : ActionNameSelectorAttribute 
{ 
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) 
    { 
     if (string.Equals("post", controllerContext.HttpContext.Request.RequestType, StringComparison.OrdinalIgnoreCase)) 
     { 
      if (methodInfo.CustomAttributes.Where(x => x.AttributeType == typeof(HttpPostAttribute)).Any()) 
      { 
       return false; 
      } 
     } 
     return controllerContext.IsChildAction; 
    } 
} 

하고 두 가지 작업을 장식 : 당신이 POST Index2 작업 이름을 변경할 수없는 경우 현재 요청이 POST 요청이있는 경우에도 사용자는 GET Index2 조치의 사용을 강제하는 사용자 지정 작업 이름 선택기를 쓸 수 그것 :

[HttpPost] 
[PreferGetChildActionForPost] 
public ActionResult Index2(string t) 
{ 
    return PartialView("Index"); 
} 

[ChildActionOnly] 
[PreferGetChildActionForPost] 
public ActionResult Index2() 
{ 
    return PartialView(); 
} 
+0

고마워, 나는 그것이 도움이 될 것 같아요. 그러나 실제로이 동작이 기본으로 사용되지 않는 이유를 이해할 수 없습니다. –

관련 문제