2013-09-22 4 views
1

내 컨트롤러는 다음과 같습니다Action 메서드를 직접 호출하여 전송?

public ActionResult Action1(Action1Model model) 
{ 
    ..... 
    if (...) 
     return Action2(new Action2Model() { .... }); //** 
    else 
     return View(model); 
} 

public ActionResult Action2(Action2Model model) 
{ ... } 

는 기본적으로 조치 1에서 특정 조건 하에서 내가 대신 조치 2로 처리를 전송할. 위의 코드는 오류 : The model item passed into the dictionary is of type 'Action2Model', but this dictionary requires a model item of type 'Action1Model'을 제공합니다. 복잡한 모델을 가질 수 없습니다,

return RedirectToAction("Action2", new { parm1 = ..., parm2 = ... ...}); 

하지만이 방법은 쿼리 문자열에있는 모든 매개 변수를, 302 (추가 HTTP 호출)를 반환 노출 :

나는 그것이 ** 줄에서이를 사용하여 작업 할 수 있습니다 , 경로 값을 채울 때 유형 검사가 없습니다.

쿼리 문자열에 모델 세부 정보를 표시하지 않고 작업을 전송하는 좋은 방법이 있습니까?

답변

2

View을 호출 할 때보기 이름을 지정하지 않으면 ASP.NET MVC가 원래 작업 이름을 기반으로보기를 찾으려고 시도합니다.

그래서 귀하의 경우는 Action2 실행하고 있지만 당신은이 예외가 발생하여 Action2ModelAction1.cshtml을 사용하려고합니다 Action2.cshtml MVC를 표시합니다.

당신은 명시 적으로 행동 뷰 이름을 쓰는이 문제를 해결 할 수 있습니다

public ActionResult Action1(Action1Model model) 
{ 
    //.... 
    if (...) 
     return Action2(new Action2Model() { .... }); //** 
    else 
     return View("Action1", model); 
} 

public ActionResult Action2(Action2Model model) 
{ 
    //... 
    return View("Action2", model); 
} 
+0

감사합니다. 그것은 아름답게 작동합니다! –

관련 문제