2013-06-03 1 views
2

양식, 두 개의 텍스트 상자 및 제출 단추를 포함하는 사용자 권한 부여에 대한 제어 권한이 있습니다. 이 컨트롤은 RenderAction 메서드를 통해 마스터 페이지에 포함됩니다. 등록 페이지 (RenderBody 메서드를 통해 뷰에 포함 된 뷰)도 양식과 함께 있습니다. 등록 양식에서 데이터를 제출하면 로그인 컨트롤이 트리거되고 처리기 (POST 데이터 처리를위한 컨트롤러 메서드)가 호출됩니다. 아래에서 권한 부여를위한 컨트롤러 메소드를 볼 수 있습니다. 다른 양식의 데이터를 제출 한 후 POST 데이터를 로그인 컨트롤로 보내지 못하게하려면 어떻게해야합니까?ASP.NET MVC 3 양식 제출 RenderAction 처리기에 영향을 미침

 [HttpPost] 
     public RedirectResult LogIn(AuthViewModel authResult) 
     { 
      if (ModelState.IsValid) 
      { 
       userService.LogInUser(authResult.Login, authResult.Password, Request.UserHostAddress); 
      } 
      else 
      { 
       TempData["AuthMessage"] = GetValidationMessage(); 
      } 
      string redirectUrl = "/"; 
      if (Request.UrlReferrer != null) 
      { 
       redirectUrl = Request.UrlReferrer.AbsoluteUri.ToString(); 
      } 
      return Redirect(redirectUrl); 

     } 

     [HttpGet] 
     [ChildActionOnly] 
     public PartialViewResult LogIn() 
     { 
      if (userService.IsUserLoggedIn()) 
      { 
       User currentUser = userService.GetLoggedInUser(); 
       ViewBag.LoggedInMessage = currentUser.FullName + "(" + currentUser.Login + ")"; 
      } 
      return PartialView("AuthControl"); 
     } 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <title>@ViewBag.Title</title> 
</head> 

<body> 
    <div> 
     <div id="header"> 
      <div> 
       <div> 
        @{Html.RenderPartial("SearchControl");} 
       </div> 
      </div> 
     </div> 
     <div id="right_menu"> 
      <div> 
       @{Html.RenderAction("LogIn", "Navigation");} 
      </div> 
       @{Html.RenderAction("Menu", "Navigation");} 
      <div> 
       @{Html.RenderAction("Index", "Messages");} 
      </div> 
      <div> 
       @{Html.RenderAction("TagCloud", "Navigation");} 
      </div> 
     </div> 
     <div id="main_content"> 
      @RenderBody() 
     </div> 
     <div id="footer"> 
     </div> 
    </div> 
</body> 
</html> 

AuthControl :

@model AuthViewModel 

<div class="rounded-corners auth-panel"> 
    @if (ViewBag.LoggedInMessage == null) 
    { 
     <div class="auth-container"> 
      @using (Html.BeginForm("LogIn", "Navigation")) 
      { 
       <div> 
        <label for="Login"> 
         Login: 
        </label> 
        @Html.TextBoxFor(m => m.Login, new { @class="middle-field"}) 
       </div> 
       <div> 
        <label for="Password"> 
         Password: 
        </label> 
        @Html.PasswordFor(m => m.Password, new { @class="middle-field"}) 
       </div> 
       <div class="in-center"> 
        <input type="image" src="@Url.Content("~/Content/Images/submit.png")"/> 
       </div> 
      } 
     </div> 

     <div class="error-msg"> 
      @if (TempData["AuthMessage"] != null) 
      { 
       @Html.Raw(TempData["AuthMessage"].ToString()) 
      } 
      @Html.ValidationSummary() 
     </div> 
     <div class="small-nav-message"> 
      <a href="#" class="registration-link">Registration</a> 
     </div> 
    } 
</div> 

등록 페이지 : 등록 감안

RegistrationViewModel 

@{ 
    ViewBag.Title = "Registration"; 
} 
@if (TempData["RegistrationFinished"] == null || !(bool)TempData["RegistrationFinished"]) 
{ 
<div class="post content-holder"> 
    <div class="fields-holder"> 
     <div > 
      <div class="error-msg"> 
       @if (TempData["ValidationMessage"] != null) 
       { 
        @Html.Raw(TempData["ValidationMessage"].ToString()) 
       } 
      </div> 
      @using (Html.BeginForm()) 
      { 
       <span> 
        Email: 
       </span> 
       <span> 
        @Html.TextBoxFor(v => v.Email) 
       </span> 
       <span> 
        Password: 
       </span> 
       <span> 
        @Html.PasswordFor(v => v.Password) 
       </span> 
       <input type="submit" value="Register"/> 
      } 
      </div> 
    </div> 
</div> 
} 
else 
{ 
    <div> 
     Activation link was sent to your email. 
    </div> 
} 
+0

[여기에 명시된 바와 같이] (http://www.w3.org/MarkUp/html3/forms.html) "양식 요소를 중첩 할 수 없습니다." – Igarioshka

+0

제 경우에는 양식이 중첩되지 않습니다. – eternity

+0

마크 업을 게시 하시겠습니까? – Igarioshka

답변

0

내 문제를 해결하기 위해 컨트롤러 컨텍스트에서 IsChildAction 속성을 확인합니다. 또한 모델 상태를 정리해야합니다.

 [HttpPost] 
     public ActionResult LogIn(AuthViewModel authResult) 
     { 
      if (!this.ControllerContext.IsChildAction) 
      { 
       if (ModelState.IsValid) 
       { 
        userService.LogInUser(authResult.Login, authResult.Password, Request.UserHostAddress); 
       } 
       else 
       { 
        TempData["AuthMessage"] = GetValidationMessage(); 
       } 
       string redirectUrl = "/"; 
       if (Request.UrlReferrer != null) 
       { 
        redirectUrl = Request.UrlReferrer.AbsoluteUri.ToString(); 
       } 
       return Redirect(redirectUrl); 
      } 
      ModelState.Clear(); 
      return PartialView("AuthControl"); 
     } 
0

변경,

@using (Html.BeginForm()) 

,745 행
@using (Html.BeginForm("Index", "Registration")) 

단일 컨트롤러의 단일 동작 시나리오에서는 추가 특정 라우팅 정보가 필요하지 않지만 라우팅 엔진은 다중 컨트롤러/동작으로 라우팅 할 컨트롤러/동작을 자체적으로 알 수 없습니다. 의견에 따라

편집 :

그래서이 라우팅 문제입니다. 등록 작업에 특정 경로를 추가하십시오. 예 :

routes.MapRoute(
    "Register", // Route name 
    "{controller}/Index/{registrationResult}", // URL with parameters 
    new { 
     controller = "{controller}", 
     action = "Selector", 
     registrationResult= UrlParameter.Optional 
     } 
); 

'registrationResult'는 게시 작업의 매개 변수 이름입니다. 보기 모델이 라우팅 엔진이 두 모델을 구별 할 수 없을만큼 매우 유사하다고 생각합니다. 위의 경로를 기본 경로 인 앞에 추가하면 등록 양식을 제출할 때 일치해야합니다.

+0

추가 정보를 추가했지만 불행히도 같은 결과가 나타납니다. – eternity

+0

@eternity 양식을로드하고 두 양식의 작업 속성을 확인할 수 있습니까? 내가보고 싶은 다른 점은 Global.asax의 RegisterRoutes 메서드입니다. –

+0

기본 경로 {controller}/{action}/{id} 만 있습니다. 등록 양식은

eternity