// 使用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>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)