验证错误消息未在视图

时间:2017-05-19 09:03:58

标签: asp.net-mvc html5 kendo-ui kendo-window

我已经在我的MVC页面中实现了一个mvc kendo窗口控件,并且表单验证doenst似乎有效。我可以在控制器中将模型状态设置为false,但视图似乎没有显示消息。请注意,WorkLogType,WorkLog主题和详细信息是必填字段。我在视图模型中设置了必需的属性。我还在视图中为各个控件设置了验证标记。有什么问题?

这是我的代码

查看

@using (Ajax.BeginForm("ActivityWorkLog_Create", "Activity", new AjaxOptions
{
    HttpMethod = "POST",
    OnSuccess = "OnSuccess",
    OnFailure = "OnFailure"
}))
{

<div class="k-popup-edit-form k-window-content k-content" data-role="window">
    <div class="k-edit-form-container">
        @Html.HiddenFor(x => x.RequestID, new { data_bind = "value: requestId" })
        @Html.HiddenFor(x => x.ActivityID, new { data_bind = "value: activityId" })
        @Html.HiddenFor(x => x.CountryCode, new { data_bind = "value: countryCode" })

         <div class="form-group">
            <div class="editor-label">
                @Html.LabelFor(model => model.WorkLogAppliesToName)
            </div>
            <div class="editor-field">
                @(Html.Kendo().ComboBoxFor(model => model.WorkLogAppliesToName)
        .Name("WorkLogAppliesToID")
        .Filter("contains")
        .HtmlAttributes(new { style = "width:300px", @readonly = "readonly" })
        .Placeholder("Select...")
        .DataTextField("WorkLogAppliesToName")
        .DataValueField("WorkLogAppliesToID")

        .DataSource(dataSource => dataSource
            .Read(read => read.Action("GetWorkLogAppliesTo", "WorkLog", new { id = 0 }).Type(HttpVerbs.Post)
            )

        )
                )
                @Html.ValidationMessageFor(model => model.WorkLogAppliesToName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="editor-label">
                @Html.LabelFor(model => model.ActivitySLA)
            </div>
            <div class="editor-field">
                @*@Html.EditorFor(model => model.ActivitySLA)*@
                @Html.TextBoxFor(model => model.ActivitySLA, new { id = "ActivityDesc", @readonly = "readonly", Class = "textBoxFor" })

            </div>
        </div>

        <div class="form-group">
            <div class="editor-label">
                @Html.LabelFor(model => model.ActivityID)
            </div>
            <div class="editor-field">
                @(Html.Kendo().ComboBoxFor(model => model.ActivityID)
    .Name("Activity")
    .Filter("contains")
    .HtmlAttributes(new { style = "width:300px", @readonly = "readonly" })
    .Placeholder("Select...")
    .DataTextField("Description")
    .DataValueField("ActivityID")

    .DataSource(dataSource => dataSource
        .Read(read => read.Action("GetActivity", "WorkLog").Data("additionalActivityInfo").Type(HttpVerbs.Post)
        )//.ServerFiltering(true)
    )//.CascadeFrom("ServiceID").Filter("contains")

                )
                @Html.ValidationMessageFor(model => model.ServiceID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="editor-label">
                @Html.LabelFor(model => model.WorkLogType)
            </div>
            <div class="editor-field">
                @(Html.Kendo().ComboBoxFor(model => model.WorkLogType)
    .Name("WorkLogTypeCode")
    .Filter("contains")
    .HtmlAttributes(new { style = "width:300px" })
    .Placeholder("Select...")
    .DataTextField("WorkLogType")
    .DataValueField("WorkLogTypeCode")

    .DataSource(dataSource => dataSource
    .Read(read => read.Action("GetWorkLogType", "WorkLog").Data("additionalWLTInfo").Type(HttpVerbs.Post))
    )

                )
                @Html.ValidationMessageFor(model => model.WorkLogType, "Please select a worklog type", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="editor-label">
                @Html.LabelFor(model => model.WorkLogSubject)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.WorkLogSubject)
                @Html.ValidationMessageFor(model => model.WorkLogSubject, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="editor-label">
                @Html.LabelFor(model => model.WorkLogDetails)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model.WorkLogDetails, new { htmlAttributes = new { @class = "form-control", cols = "50" } })
                @Html.ValidationMessageFor(model => model.WorkLogDetails, "", new { @class = "text-danger" })
            </div>
        </div>

            <div class="worklogStatusButtonAlign">
                <button id="btnWorkLogSave" type="submit" class="k-button k-button-icontext k-primary k-grid-update">Save</button>
                <button id="btnClose" type="button" class="k-button k-button-icontext k-grid-cancel">Cancel</button>

            </div>

            <div id="statusMessage"> </div>

        </div>
</div>
}

视图模型

 public class ActivityWorkLogViewModel
    {
        [ScaffoldColumn(false)]
        [Display(Name = "WorkLogID", ResourceType = typeof(Resources.Resource))]
        public int WorkLogID { get; set; }
        [Required]
        [Display(Name = "WorkLogTypeCode", ResourceType = typeof(Resources.Resource))]
        public string WorkLogTypeCode { get; set; }
        [Display(Name = "WorkLogType", ResourceType = typeof(Resources.Resource))]

        [Required(ErrorMessage = "WorkLogType is required")]
        public string WorkLogType { get; set; }
        [Required]
        [Display(Name = "WorkLogAppliesToID", ResourceType = typeof(Resources.Resource))]
        public int WorkLogAppliesToID { get; set; }
        [Display(Name = "WorkLogAppliesToName", ResourceType = typeof(Resources.Resource))]
        public string WorkLogAppliesToName { get; set; }
        [Required]
        [Display(Name = "RequestID", ResourceType = typeof(Resources.Resource))]
        public int RequestID { get; set; }
        [Display(Name = "ServiceID", ResourceType = typeof(Resources.Resource))]
        public Nullable<int> ServiceID { get; set; }
        [Display(Name = "ActivityID", ResourceType = typeof(Resources.Resource))]
        public Nullable<int> ActivityID { get; set; }
        [Required(ErrorMessage = "Subject is required")]
        [Display(Name = "WorkLogSubject", ResourceType = typeof(Resources.Resource))]
        public string WorkLogSubject { get; set; }
        [Required(ErrorMessage = "Details is required")]
        [Display(Name = "WorkLogDetails", ResourceType = typeof(Resources.Resource))]
        public string WorkLogDetails { get; set; }
        [Display(Name = "EmailTo", ResourceType = typeof(Resources.Resource))]
        public string EmailTo { get; set; }
        [Display(Name = "IsActive", ResourceType = typeof(Resources.Resource))]
        public bool IsActive { get; set; }
        [Display(Name = "CountryCode", ResourceType = typeof(Resources.Resource))]
        public string CountryCode { get; set; }
        [Display(Name = "ActivitySLA", ResourceType = typeof(Resources.Resource))]
        public string ActivitySLA { get; set; }
    }

控制器

[HttpPost]
        public ActionResult ActivityWorkLog_Create(ActivityWorkLogViewModel workLogViewModel)
        {
            if (!ModelState.IsValid)
            {
                return View("EditorTemplates/_WorkLogEdit", Mapper.Map<ActivityWorkLogViewModel>(workLogViewModel));
            }
            WorkLogRepository workLogRepository = new WorkLogRepository();
            workLogRepository.CreateWorkLog(Mapper.Map<WorkLog>(workLogViewModel));
            return PartialView("EditorTemplates/_WorkLogEdit", Mapper.Map<ActivityWorkLogViewModel>(workLogViewModel));
        }

Bundle.config

 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                     "~/Scripts/jquery-{version}.js",
                     "~/Scripts/jquery-ui-1.12.1.min.js",
                     "~/Scripts/jquery.validate.min.js",
                     "~/Scripts/jquery.validate.unobtrusive.min.js"
                     ));

1 个答案:

答案 0 :(得分:0)

在视图中添加以下行:

<script>
    $(document).ready(function() {
        var form = $('form') // Give Id prop to your from
            .removeData("validator")
            .removeData("unobtrusiveValidation");

        $.validator.unobtrusive.parse(form);
    });
<script>

另外,请务必将jquery.validate.jsjquery.validate.unobtrusive.js添加到View cshtml中,以便验证工作正常。