如何解决MVC验证错误

克里希纳·索尼(Krishna Soni)
 public String Name {Get;Set;} 
 Public String LastName {Get;Set;} 
 Public String Age {Get;Set;} 
 [required]
 public Comment{Get;Set;}

        <body>
        <table>
        <tr>
        <td>
        @html.textboxfor(m=>m.name)
        </td>
        <td>
        <input type="submit">Search</input>
        </td>
        <tr>
        </table>
    <table>
    <tr>
    <td>
        @html.textboxfor(m=>m.comment)
    </td>
 <td>
        <input type="submit">Save</input>
        </td>
    </tr>
    </table>
        </body>

当单击“按钮”时,模型差异将在“注释”上应用,但在“保存Buuton单击验证”是数据注释验证时,将对库套进行验证

甘西安·辛格(Ghanshyam Singh)

您必须像这样使用Jquery Unobtrutrusive验证进行即时验证;

型号类别

public class UserModel
{
   [Required(ErrorMessage ="Name is Mandatory")]
   public string FirstName {get;set;}
   [Required(ErrorMessage ="Last Name Field is Mandatory")] 
   Public string LastName {get;set;} 
   Public string Age {get;set;} 
   [required]
   public Comment{get;set;}
}

在您的CsHtml文件中,请包含Thes库

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

之后,更改您的查看代码并添加ValidateMessageFor以显示错误

@Html.TextBoxFor(m=>m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName, null, new { @class = "" })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章