数据验证无法在视图中工作(DataAnnotations)

时间:2013-05-06 17:49:20

标签: asp.net-mvc validation model-view-controller data-annotations

我无法在我的视图中进行数据验证。 我相信如果用户选中文本框并且没有输入任何数据,但是当文本框失去焦点时,“AssetName”应显示错误消息。

型号:

using System.ComponentModel.DataAnnotations;

namespace ACore.Models
{
    public class AssetForm
    {
        [Required]
        public string AssetName { get; set; }

        public string Address1 { get; set; }

        public string Address2 { get; set; }

        public string City { get; set; }

        [UIHint("StatesEditor")]
        public State State { get; set; }

        [Required(ErrorMessage = "TEST")]
        public string ZipCode { get; set; }

        [Required]
        public string Seg3Code { get; set; }
    }
}

查看:

@using System.Web.Optimization
@model ACore.Models.AssetForm

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "frmAsset" }))
{
    <div class="tempStyle">
        <div class="editor-label fl">
            @*@Html.LabelFor(model => model.AssetName)*@
            Asset Name:
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AssetName)
            @Html.ValidationMessageFor(model => model.AssetName)
        </div>
    </div>

    <div class="tempStyle">
        <div class="editor-label">
            Address 1:
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address1)
            @Html.ValidationMessageFor(model => model.Address1)
        </div>
    </div>
}

Web.config的部分

<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

我最后添加了以下代码,但似乎我班级的DataAnnotations与验证无关。我更愿意通过DataAnnotations来控制验证:

// Validate the Maintenance and Office percentages.
    var validator = $("#frmAsset").validate({
        rules: {
            AssetName: {
                required: true
            },
            Seg3Code: {
                required: true,
                minlength: 3,
                maxlength: 3
            }
        },
        messages: {
            AssetName: "&nbsp;&#10006;&nbsp;Required",
            Seg3Code: "&nbsp;&#10006;&nbsp;Required"
        },
        submitHandler: function (form) {

            $.ajax({
                type: "POST",
                url: "/Asset/Create",
                data: {

                },
                //data: $("form").serialize(),
                success: function (data) {
                    //console.log(data);

                    // Close popup window
                    var window = $('#AssetEditorPopUp').data("kendoWindow");
                    window.close();

                    // Refresh grid to show changes
                    $('#grid').data("kendoGrid").dataSource.read();
                },

                error: function () {
                    alert("There was an error editing the asset.");
                }
            });

            return false; // to block page redirect since you're using ajax
        }

1 个答案:

答案 0 :(得分:0)

在您的BeginForm中,没有导致提交的发布操作,因此也没有调用验证。除非您有一些在制表符上验证的Javascript代码,否则这是必需的。

在您使用的代码块中添加

<input type="submit" value="textOnSubmitButton" />

并在BeginForm函数中提供操作和控制器,以处理在控制器中发布表单的操作。 在控制器中添加它

[HttpPost]
public ActionResult theAction(){
    return View();
}

点击提交按钮

会收到错误消息