MVC笔记1
测试实体类
public class Test { public int id{get;set;} [Required(ErrorMessage="不能为空喔")] [StringLength(10,ErrorMessage="字符串不能超过10")] public string titlenew{get;set;} }
使用@using(Html.BeginForm("form")) {} 生成的代码 带有data-格式的验证信息
@using(Html.BeginForm("form")){
@Html.TextBoxFor(m => m.titlenew);
@Html.ValidationMessage("titlenew");
<input type="submit" value="提交"/>
}
生成后代码:
<form action="/AutoComplete?Length=4" method="post">
<input data-val="true" data-val-length="字符串不能超过10" data-val-length-max="10" data-val-required="不能为空喔" id="titlenew" name="titlenew" type="text" value="001" />
<span class="field-validation-valid" data-valmsg-for="titlenew" data-valmsg-replace="true"></span>
<input type="submit" value="提交"/> </form>
而直接使用<form>
<form id="f2" action="@Url.Action("form")"> @Html.TextBoxFor(m=>m.titlenew) @Html.ValidationMessage("titlenew"); <input type="submit" value="提交"/> </form>
生成后代码:
<form id="f2" action="/AutoComplete/form"> <input id="titlenew" name="titlenew" type="text" value="001" /> <input type="submit" value="提交"/> </form>
所以,在提交表单的时候,如果需要后台进行验证,就需要使用@using(Html.BeginForm("form")) {} 了。
标签:
MVC
, mvc beginform
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通