简体   繁体   English

如何在一个视图上处理来自多个强类型可提交部分视图的回发?

[英]How to deal with postbacks from multiple strongly-typed submit-able partial views on one view?

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<EAZYITT_LOGIN.Models.CombinedViewModel>" %>
 <asp:Content ContentPlaceHolderID="TitleContent" runat="server">
    LoginPage
 </asp:Content>         
 <asp:Content ContentPlaceHolderID="MainContent" runat="server">         
     <div id="LoginWindow">    
         <% Html.RenderPartial("LoginWindow", ViewData.Model.Logon); %>                 
         <a id="ForgetPassword" href="#" onclick="loadSegment()">Forgot  Password</a>
     </div>         
     <div id="PassReminderWindow">
         <% Html.RenderPartial("ReminderWindow", ViewData.Model.Reminder); %>                 
     </div>
 </asp:Content>

Each partial view is strongly typed with separate postbacks to the server 每个部分视图都具有强烈的类型,并带有单独的服务器回发

Login: 登录:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EAZYITT_LOGIN.Models.LogOnModel>" %>     
<div id="loginPage">
     <h2>Login Page</h2>     
     <h3>Submit your credentials to continue or register</h3>             

     <%: Html.ActionLink("New Registration", "Register")%>     
     <%: Html.ValidationSummary(true)%>
     <%: Html.ValidationSummary()%>     

     <div class="validation-summary-errors">
         <span id="loginError"></span>
     </div>

     <% using (Html.BeginForm("LoginWindow","Account",FormMethod.Post)) { %>

     <%:Html.LabelFor(m =>m.EmailAddress) %>
     <%:Html.ValidationMessageFor(m => m.EmailAddress) %>
     <%:Html.TextBoxFor(m => m.EmailAddress) %>

     <%:Html.LabelFor(m =>m.Password) %>
     <%:Html.ValidationMessageFor(m => m.Password) %>
     <%:Html.PasswordFor(m => m.Password)%>

     <%:Html.CheckBoxFor(m => m.RememberMe)%>
     <%:Html.LabelFor(m => m.RememberMe)%>

     <p>
         <input type="submit" value="Log On" />
     </p>

     <% } %>
</div>

Password Reminder: 密码提醒:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EAZYITT_LOGIN.Models.ReminderModel>"%>         

     <div id="PasswordReminderDiv">
         <h2>PasswordReminder</h2>

         <%: Html.ValidationSummary(true) %>
         <div class="validation-summary-errors"><span
 id="reminderError"></span></div>

         <% using (Html.BeginForm("PasswordReminder",
 "Account",FormMethod.Post))
           { //'8' o-o +:: %>

           <%:Html.LabelFor(m=>m.ReminderEmailAddress) %>
           <%:Html.ValidationMessageFor(m =>
 m.ReminderEmailAddress)%>
           <%:Html.TextBoxFor(m => m.ReminderEmailAddress) %>

           <p>
             <input type="submit" value="Send Reminder" />
           </p>
         <%} %>
     </div>

The results are submitted to their separate methods in the controller: 结果将提交到控制器中各自的方法:

         [HttpGet]
         public ActionResult Login()
         {
             CombinedViewModel cModel = new CombinedViewModel();

             cModel.Logon = new LogOnModel();
             cModel.Reminder = new ReminderModel();

             return View(cModel);
         }

         [HttpPost]
         public ActionResult Login(CombinedViewModel _login)
         {
             return View(_login);
         }

         [HttpGet]
         public ActionResult LoginWindow()
         {
             return PartialView();
         }

         [HttpPost]
         public ActionResult LoginWindow(LogOnModel _login)
         {
             if (ModelState.IsValid)
             {
                 if (LoginService.ValidateUser(siteId, _login.EmailAddress, _login.Password))
                 {
                     //Goto Next Page
                     ModelState.AddModelError("loginError", "LOGIN - OK");
                 }
                 else
                 {
                     //Failed Login
                     ModelState.AddModelError("loginError", "Wrong username or password");
                 }
             }

             return PartialView("LoginWindow", _login);
         }     

         [HttpGet]
         public ActionResult PasswordReminder()
         {
             return View();
         }


         [HttpPost]
         public ActionResult PasswordReminder(ReminderModel _reminder)
         {
             TempData["ModelState"] = ModelState;
             if (LoginService.ValidateNewUser(siteId, _reminder.ReminderEmailAddress))
                 ModelState.AddModelError("reminderError", "The E-mail address does not exist");

             if (ModelState.IsValid)
             {
                 ModelState.AddModelError("reminderError", "E-mail found, send e-mail to user");     
             }

             return PartialView("PasswordReminder",_reminder);     
         }

Ideally, I would like the main view (Login.aspx) to be displayed regardless of the validation. 理想情况下,无论验证如何,我都希望显示主视图(Login.aspx)。

However, I've currently got the validation on each partial view working, BUT it is taking me to their separate partial views on failed validation as opposed to the main view. 但是,我目前已经在每个局部视图上进行了验证,但是将我带到他们各自的局部视图上以进行验证失败,而不是主视图。

How would I get it to update the partial view only? 我如何才能仅更新部分视图? Is this the way to do it or should I be using AJAX? 这是这样做的方式还是应该使用AJAX?

The only way to update the partial view only is AJAX, use AJAX.BeginForm or use JQuery. 仅更新部分视图的唯一方法是AJAX,请使用AJAX.BeginForm或JQuery。 Otherwise, it expects to go through the full lifecycle. 否则,它将期望经历整个生命周期。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM