zoukankan      html  css  js  c++  java
  • .Net MVC3.0数据验证之伙伴类的使用

    
    
    // 使用EF的时候如果修改数据库重新生成EF里的model会被覆盖,写在上面的验证规则也会被覆盖,所以用伙伴类,创建强类型视图指向 UserInfo  model 

    using
    System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace MvcApp.Models {
    public class UserInfoValidate { [Required(ErrorMessage="编号不能为空")] public int ID{ get; set; } [Required(ErrorMessage="用户名不能为空")] public string UserName { get; set; } } [MetadataType(typeof(UserInfoValidate))] public partial class UserInfo { }
    
    
    }
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApp.Models.UserInfo>" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>CreateUser</title>
        <style type="text/css">
        .txt{color:Red}
        
        </style>
    </head>
    <body>
    <%Html.EnableClientValidation(); %>
        <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
        <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
        <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
        <% using (Html.BeginForm()) {%>
            <%: Html.ValidationSummary(true) %>
    
            <fieldset>
                <legend>Fields</legend>
                
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.ID) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.ID) %>
                    <%: Html.ValidationMessageFor(model => model.ID, "", new {@class="txt" })%>
                </div>
                
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.UserName) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.UserName) %>
                    <%: Html.ValidationMessageFor(model => model.UserName) %>
                </div>
                
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.UserPass) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.UserPass) %>
                    <%: Html.ValidationMessageFor(model => model.UserPass) %>
                </div>
                
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.RegTime) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.RegTime) %>
                    <%: Html.ValidationMessageFor(model => model.RegTime) %>
                </div>
                
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Email) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Email) %>
                    <%: Html.ValidationMessageFor(model => model.Email) %>
                </div>
                
                <p>
                    <input type="submit" value="Create" />
                </p>
            </fieldset>
    
        <% } %>
    
        <div>
            <%: Html.ActionLink("Back to List", "Index") %>
        </div>
    
    </body>
    </html>
  • 相关阅读:
    JavaScript中判断函数是new还是()调用
    IE6/7 and IE8/9(Q)中td的上下padding失效
    JQuery中html()方法使用不当带来的陷阱
    有name为action的表单元素时取form的属性action杯具了
    为非IE浏览器添加mouseenter,mouseleave事件
    各浏览器中querySelector和querySelectorAll的实现差异
    仅IE6/7/8中innerHTML返回值忽略英文空格
    各浏览器关键字/保留字作为对象属性的差异
    各浏览器中鼠标按键值的差异
    给body标签和document.body都添加点击事件后仅Firefox弹出了两次
  • 原文地址:https://www.cnblogs.com/kongsq/p/5865982.html
Copyright © 2011-2022 走看看