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")) {}  了。

原文地址:https://www.cnblogs.com/tiancai/p/4843493.html