0

我已将ComponentModel.DataAnnotations[Required] 属性用于验证消息。该消息未显示在我的表单中。但是,正则表达式验证和范围验证工作正常。

在我的一个领域中,我同时使用了 [Required] 和 [RegualarExpression]。当我在文本框中输入错误时,会出现 RegualarExpression 验证消息,在删除输入后,只有必需的验证消息。否则,不会出现必需的验证消息。

这是我的代码:

--模型类

    public class Email
    {
    [Required(ErrorMessage = "Email Address is required!")]
    [RegularExpression("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]            {1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", ErrorMessage = "Invalid Email Address.")]
    public virtual string EmailAddress { get; set; }


    [Required(AllowEmptyStrings = false, ErrorMessage = "First Name is Required")]
    public virtual string FirstName { get; set; }

    public virtual string MiddleName { get; set; }
    [Required(ErrorMessage = "Last Name is required!")]
    public virtual string LastName { get; set; }

    public virtual string Status { get; set; }
    }

- 看法

    @{Html.EnableClientValidation();}

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">        </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"     type="text/javascript"></script>
    @using (Html.BeginForm("CreateEmail", "LeaderBoard", FormMethod.Post, new { enctype =   "multipart/form-data", id = "create-email-dialog" }))
    {
    @Html.ValidationSummary(true)
    {
    <form>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="popTable">
    <tr>
        <td width="38%">
            First Name:
        </td>
        <td width="62%">
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
        </td>
    </tr>
    <tr>
        <td width="38%">
            Middle Name:
        </td>
        <td width="62%">
            <div class="editor-field">
                @Html.EditorFor(model => model.MiddleName)
                @Html.ValidationMessageFor(model => model.MiddleName)
            </div>
        </td>
    </tr>
    <tr>
        <td width="38%">
           Last Name:
        </td>
        <td width="62%">
            <div class="editor-field">
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
        </td>
    </tr>
    <tr>
        <td width="38%">
            Email Address:
        </td>
        <td width="62%">
            <div class="errorMessage">
                @Html.EditorFor(model => model.EmailAddress)
                @Html.ValidationMessageFor(model => model.EmailAddress)
            </div>
        </td>
    </tr>
     <tr>
        <td width="38%">
             Status:
        </td>
        <td width="62%">
            <div class="editor-field">
                @Html.DropDownList("Status", ViewData["Status"] as SelectList)
            </div>
        </td>
    </tr>
    </table>    
    <input type="button" id="btnCreateEmail" value="Save" name="submit" class="btn" />
    <input type="button" value="Cancel" name="cancel" class="closeDialog" />
    </form>

    }
    }

- 控制器

    [HttpGet]
    public ActionResult CreateEmail()
    {
        if (Request.IsAjaxRequest())
        {
                var list = new SelectList(new[]
                {
                    new{Id = "Active", Name = "Active"},
                    new{Id = "Inactive", Name = "Inactive"}
                },
                "Id", "Name", 1);

            ViewData["Status"] = list;
            return PartialView("_CreateEmail");
        }
        return RedirectToAction("Index");
    }

    [HttpPost]
    public ActionResult CreateEmail(Email model)
    {
        using (_emailRepository)
        {
            if (ModelState.IsValid)
            {
                _emailRepository.Create(model);
                return Json(new { success = true });
            }
            return RedirectToAction("Index");
        }
    }
4

1 回答 1

0

在文档就绪函数中验证这些字段

       $( document ).ready( function ( e )
        {

$( "form" ).validate().element( "#FirstName" );


$( "form" ).validate().element( "#LastName" );
});
于 2013-03-21T11:15:08.703 回答