代码之家  ›  专栏  ›  技术社区  ›  john84

更新两个ValidationSummaries中的错误(在部分视图中)

  •  1
  • john84  · 技术社区  · 16 年前

    在我的小应用程序中,我有一个强类型的部分视图(登录表单),它在母版页中呈现,如下所示:

              <!-- Login Box -->
              <div class="login">
                <div class="login-bg clearingfix">
                  <% Html.RenderPartial("LoginViewControl", Model); %>
                </div>
              </div>
              <!-- / Login Box -->
    

    此局部视图的模型为:

    using System;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    
    using mpws.Util.Validation;
    
        namespace mpws.Models
        {
            public class LoginViewModel
            {
                [Required]
                public string UserName { get; set; }
    
                [Required]
                public string Password { get; set; }
    
            }
        }
    

    视图本身包含一个Validationsummary: <%= Html.ValidationSummary() %>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    
    using mpws.Util.Validation;
    
    namespace mpws.Models
    {
        public class SignUpViewModel
        {
            [Required]
            public string UserName { get; set; }
    
            [Required]
            public string Password { get; set; }
    
            [Required]
            public string FirstName { get; set; }
    
            [Required]
            public string LastName { get; set; }
    
            [Required]
            public string EmailAddress { get; set; }
    
            [Required]
            public string EmailAddressRepeat { get; set; }
    
            public string Occupation { get; set; }
    
            public string SuggestedBy { get; set; }
    
            public IValidationState validationState { get; set; }
    
        }
    }
    

    这里是我的注册视图:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<mpws.Models.SignUpViewModel>" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        SignUp
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <h2>SignUp</h2>
    
    <div id="SignUpForm">
        <% using (Html.BeginForm("SignUp","Account", new mpws.Models.SignUpViewModel())) {%>
            <%: Html.ValidationSummary() %>
    
            <fieldset>
                <legend>Fields</legend>
    
                <div class="editor-label">
                    <%: Html.Resource("Strings, Username") %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.UserName) %>
                    <%: Html.ValidationMessageFor(model => model.UserName) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.Resource("Strings, Password") %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Password) %>
                    <%: Html.ValidationMessageFor(model => model.Password) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.Resource("Strings, FirstName") %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.FirstName) %>
                    <%: Html.ValidationMessageFor(model => model.FirstName) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.Resource("Strings, LastName") %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.LastName) %>
                    <%: Html.ValidationMessageFor(model => model.LastName) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.Resource("Strings, EmailAddress") %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.EmailAddress) %>
                    <%: Html.ValidationMessageFor(model => model.EmailAddress) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.Resource("RepeatEmailAddress") %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.EmailAddressRepeat) %>
                    <%: Html.ValidationMessageFor(model => model.EmailAddressRepeat) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Occupation) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Occupation) %>
                    <%: Html.ValidationMessageFor(model => model.Occupation) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.SuggestedBy) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.SuggestedBy) %>
                    <%: Html.ValidationMessageFor(model => model.SuggestedBy) %>
                </div>
    
                <p>
                    <button class="button positive" type="submit"> 
                          <img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/tick.png") %>" /> 
                          <%: Html.Resource("Strings, SignUp") %>   
                    </button>
                    <button class="button negative" type="reset"> 
                          <img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/cross.png") %>" /> 
                          <%: Html.Resource("Strings, Cancel") %>  
                    </button>
                </p>
            </fieldset>
    
        <% } %>    
    </div>
    
    </asp:Content>
    

    以及登录部分视图:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<mpws.Models.LoginViewModel>" %>
    
    <!-- This should be outsourced into it own .js fila -->
    <script type="text/javascript">
        /// <reference path="jquery-1.4.1-vsdoc.js" />
    
        $(document).ready(function () {
            $('#LoginForm form').live('submit', function () {
                $.post($(this).attr('action'), $(this).serialize(), function (result) {
                    if ($.isString(result)) {
                        $("#LoginForm").replaceWith($(result));
                    }
                    else if (result["Successfull"]) {
                        window.location.href = result["RedirectUrl"];
                    }
                    else {
                        alert('Unknown Error...');
                    }
                });
                return false;
            });
        });
    
        jQuery.isString = function (o) {
            return (typeof o === "string");
        }
    </script>
    
    <div id="LoginForm">
        <% using (Html.BeginForm("SignIn", "Account"))
           {%>
    
            <fieldset>
                <legend>Login</legend>
                <%if(!ViewContext.ViewData.ModelState.IsValid) {%>
                    <%= Html.ValidationSummary_jQueyUIfriendly() %>
    
                    <%= Html.ValidationSummary() %>
                    <br />  
                <%} %>
    
                <div class="editor-label">
                    <%: Html.Resource("Strings, Username") %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.UserName)%> <br />
                    <%: Html.ValidationMessageFor(model => model.UserName)%>
                </div>
    
                <div class="editor-label">
                    <%: Html.Resource("Strings, Password") %>
                </div>
                <div class="editor-field">
                    <%: Html.PasswordFor(model => model.Password)%><br />
                    <%: Html.ValidationMessageFor(model => model.Password)%>
                </div>
                <p>
                    <button class="button positive" type="submit"> 
                          <img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/tick.png") %>" /> 
                          <%: Html.Resource("Strings, SignIn") %>
                     </button>
                </p>
            </fieldset>
    
        <% } %>
    </div>
    

    知道吗?

    提前谢谢 约翰内斯

    0 回复  |  直到 16 年前