MVC客户端验证? [英] MVC Client side validation?

查看:133
本文介绍了MVC客户端验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个文本框...一个天,第二个用于月份和第三的一年。我想使用MVC的验证检查,如果该领域的一个空白,然后显示*。是否有可能在按钮提交仅显示一个错误信息,如果这些字段之一是空的?

 < D​​IV CLASS =表单组>
    <标签=出生日期级=控制标签COL-LG-5>
        @ Html.Label(@ BetXOnline.TranslationProvider.Instance.GetTranslationMessage(BIRTHDATE)):
        @ Html.ValidationMessage(*)
    < /标签>
    < D​​IV CLASS =COL-LG-2>
        @ Html.TextBoxFor(M = GT; m.Register.Day,新{ID =day_birthdate,@class =表格控})
    < / DIV>
    < D​​IV CLASS =COL-LG-2>
        @ Html.TextBoxFor(M = GT; m.Register.Month,新{ID =month_birthdate,@class =表格控})
    < / DIV>
    < D​​IV CLASS =COL-LG-3>
        @ Html.TextBoxFor(M = GT; m.Register.Year,新{ID =year_birthdate,@class =表格控})
    < / DIV>


解决方案

在你的模块类的属性附加与文本框提供属性 [必需]

不是你可以使用的ValidationSummary的这已经是Asp.net MVC使用。

如果您希望只显示文件的旁边比消息确实使用 ValidationMessageFor

检查: ASP.NET MVC客户端端验证

这个类属性定义

  [必需(的ErrorMessage =*)]
    公共字符串名称{;组; }

示例

 < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Email)
        @ Html.ValidationMessageFor(型号=> model.Email)
    < / DIV>

I have 3 textboxes ...one for day, second for month and third for year. I want to use mvc validation to check if one of this field is empty and then show *. Is it possible on button submit display just one error message if one of those fields are empty?

<div class="form-group">
    <label for="dateofbirth" class="control-label col-lg-5">
        @Html.Label(@BetXOnline.TranslationProvider.Instance.GetTranslationMessage("BIRTHDATE")):
        @Html.ValidationMessage("*")
    </label>
    <div class="col-lg-2">
        @Html.TextBoxFor(m => m.Register.Day, new { id = "day_birthdate", @class = "form-control" })
    </div>
    <div class="col-lg-2">
        @Html.TextBoxFor(m => m.Register.Month, new { id = "month_birthdate", @class = "form-control" })
    </div>
    <div class="col-lg-3">
        @Html.TextBoxFor(m => m.Register.Year, new { id = "year_birthdate", @class = "form-control" })
    </div>

解决方案

In your module class on the property attached with textbox provide attribute [Required].

than you can use of ValidationSummary which is already available in Asp.net MVC.

if you want to display message just beside file than do use ValidationMessageFor

Check : ASP.NET MVC Client Side Validation

class property definition for this

    [Required(ErrorMessage = "*")]
    public string Name { get; set; }

Example

<div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div> 

这篇关于MVC客户端验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆