ASP .NET MVC 5 Html.ValidationMessageFor not working

时间:2018-04-20 04:40:13

标签: c# asp.net asp.net-mvc validation asp.net-mvc-5

以下是.cshtml代码:

using (Ajax.BeginForm("UploadEdit", "Widget", new { Type = "SampleSurvey" }, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "SampleSurvey-widget", LoadingElementId = "report-loader", OnBegin = "$('#report-loader').show();", OnSuccess = "$('#report-loader').hide();" }, new { enctype = "multipart/form-data", id = "SampleSurvey" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Type)
    @Html.HiddenFor(model => model.IsDealer)
    @Html.HiddenFor(model => model.Filter)

    <div class="form form--searchcriteria">
        <fieldset class="">
            <div class="field_group">
                <div class="field">
                    @Html.LabelFor(Model => Model.FirstName, new { @class = "field__label" })
                    <div class="field__controls">
                        @Html.EditorFor(model => model.FirstName)
                        @Html.ValidationMessageFor(model => model.FirstName)
                    </div>
                </div>
                <div class="field">
                    @Html.LabelFor(model => model.LastName, new { @class = "field__label" })
                    <div class="field__controls">
                        @Html.EditorFor(model => model.LastName)
                        @Html.ValidationMessageFor(model => model.LastName)
                    </div>
                </div>
                <div class="field">
                    @Html.LabelFor(model => model.Email, new { @class = "field__label" })
                    <div class="field__controls">
                        @Html.EditorFor(model => model.Email)
                        @Html.ValidationMessageFor(model => model.Email)
                    </div>
                </div>
            </div>
        </fieldset>         
        <div class="form-control">
            <button id="btnSubmit" class="btn icon icon-location-arrow" type="submit" onclick="$('#report-loader').show();">Send invitation</button>
        </div>

    </div>
}

以下是我的模特:

public class SampleSurvey : WidgetBase
{      
    [Required]
    [StringLength(100, ErrorMessage = "Maximum 100 characters are allowed in field")]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Maximum 100 characters are allowed in field")]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress, ErrorMessage = "Invalid field.")]
    [Display(Name = "Email Address")]
    public string Email { get; set; }      

}

但是点击提交按钮后仍然无法验证,非常感谢帮助。

0 个答案:

没有答案