2016-01-21 51 views
0

我有兩種形式在ASP MVC 5中創建的網頁。 Screan of pageASP MVC 5 - 頁上的兩種形式

第一個 「LoginPanel」 是局部視圖,類似這樣的

@model UowTest.Models.LoginPanelModel 

@using (Html.BeginForm("LoginPanel", "Account", FormMethod.Post, new { @class = "navbar-form navbar-left" })) 
{ 
    if(!Model.IsLogin) 
    { 
     <div class="form-group"> 
      @Html.TextBoxFor(m => m.Login, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.Login) }) 
     </div> 
     <div class="form-group"> 
      @Html.PasswordFor(m => m.Password, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.Password) }) 
     </div> 
     <button type="submit" class="btn btn-default">Zaloguj</button> 
    } 
    else 
    { 
     <ul class="nav navbar-nav navbar-left"> 
      <li><span>Witaj</span></li> 
      <li><a href="#">@Model.UserName</a></li> 
     </ul> 
    } 
} 

被添加到_Layout.cshtml在頂板部:

<body> 
    <div class="navbar navbar-inverse navbar-fixed-top"> 
     <div class="container"> 
      <div class="navbar-header"> 
      ... 
      </div> 
      <div class="navbar-collapse collapse"> 
       <ul class="nav navbar-nav navbar-right"> 
        <li>@Html.Action("LoginPanel", "Account")</li> 
       </ul> 
      </div> 
     </div> 
    </div> 
</body> 
中的AccountController

我有HTTP GET和Post方法:

public ActionResult LoginPanel() 
{ 
    return PartialView(new LoginPanelModel()); 
} 

[HttpPost] 
public ActionResult LoginPanel(LoginPanelModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     return RedirectToAction("Index", "Home"); 
    } 

    return RedirectToAction("Login", new { message = "Login lub hasło jest nieprawidłowe!" }); 
} 

它工作得很好,直到我說像註冊視圖:

@model UowTest.Models.RegisterModel 

@{ 
    ViewBag.Title = "Register"; 
} 

@using (Html.BeginForm("Register", "Account", FormMethod.Post)) 
{ 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Login) 
     @Html.TextBoxFor(m => m.Login, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.Login) }) 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Password) 
     @Html.PasswordFor(m => m.Password, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.Password) }) 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.RepeatedPassword) 
     @Html.PasswordFor(m => m.RepeatedPassword, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.RepeatedPassword) }) 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.FirstName) 
     @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.FirstName) }) 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.LastName) 
     @Html.TextBoxFor(m => m.LastName, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.LastName) }) 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Email) 
     @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = Html.PlaceholderFor(m => m.Email) }) 
    </div> 
    <button type="submit" class="btn btn-default">Zarejestruj</button> 
} 

用的AccountController方法:

public ActionResult Register() 
{ 
    return View(new RegisterModel()); 
} 

[HttpPost] 
public ActionResult Register(RegisterModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     return RedirectToAction("Login"); 
    } 

    return View(model); 
} 

當我擊註冊按鈕,它會調用

[HttpPost] 
public ActionResult Register(RegisterModel model) 

當ModelState中不有效的我返回寄存器視圖來顯示驗證消息,但insted的看到這個頁面,有什麼叫

[HttpPost] 
public ActionResult LoginPanel(LoginPanelModel model) 

什麼原因錯誤:

System.Web.HttpException was unhandled by user code 
    HResult=-2147467259 
    Message=Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. 
    Source=System.Web 
    ErrorCode=-2147467259 
    WebEventCode=0 
    StackTrace: 
     w System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) 
     w System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) 
     w System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) 
     w System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) 
     w System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) 
     w System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) 
     w System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName) 
     w ASP._Page_Views_Shared__Layout_cshtml.Execute() w c:\Users\Lenovo T430s\Documents\Visual Studio 2013\Projects\UowTest\UowTest\Views\Shared\_Layout.cshtml:wiersz 30 
     w System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
     w System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
     w System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
     w System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer) 
     w System.Web.WebPages.WebPageBase.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) 
     w System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) 
     w System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content) 
     w System.Web.WebPages.WebPageBase.Write(HelperResult result) 
     w System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) 
     w System.Web.WebPages.WebPageBase.PopContext() 
     w System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
     w System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) 
     w System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) 
     w System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) 
     w System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 
     w System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
     w System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
    InnerException: System.InvalidOperationException 
     HResult=-2146233079 
     Message=Child actions are not allowed to perform redirect actions. 
     Source=System.Web.Mvc 
     StackTrace: 
      w System.Web.Mvc.RedirectToRouteResult.ExecuteResult(ControllerContext context) 
      w System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 
      w System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
      w System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
      w System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) 
      w System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() 
      w System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
      w System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) 
      w System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) 
      w System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
      w System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) 
      w System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) 
      w System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) 
      w System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
      w System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) 
      w System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) 
      w System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) 
      w System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) 
      w System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
      w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() 
      w System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) 
      w System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) 
      w System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) 
      w System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) 
      w System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.<>c__DisplayClassa.<EndProcessRequest>b__9() 
      w System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.<>c__DisplayClass4.<Wrap>b__3() 
      w System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func) 
      w System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap(Action action) 
      w System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result) 
      w System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) 
     InnerException: 

在_Layout.cshtml符合在其中創建登錄局部視圖。

回答

0

嘗試返回PatrialView而不是視圖。

[HttpPost] 
public ActionResult Register(RegisterModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     return RedirectToAction("Login"); 
    } 

    return PartialView(model); 
} 
+0

這不是一個解決方案,因爲當我返回PartialView頁面不使用我的layaut和註冊頁面是醜陋的,但錯誤沒有發生。 – user1988350

+0

你爲什麼不讓註冊表成爲局部視圖,從你的圖像看來,註冊是一個局部視圖。 – SadiRubaiyet

+0

註冊是標準視圖並使用_Layout女巫包含LoginPanel局部視圖。註冊爲部分視圖也許可以解決我的問題,但需要使用表單作爲部分視圖,因爲所有視圖都使用_Layout。我認爲這個問題是在LoginPanel post方法中重定向的,但是如何在模型無效後做重定向。 – user1988350