EF6 CF邮政编码验证从零开始" 0"

时间:2016-07-11 03:07:13

标签: jquery asp.net-mvc entity-framework

您好我已经使用EF6验证了。

list1

地址是:156 Main St 贝尔法斯特,ME 04915,美国

邮政编码从零开始

但是当MVC验证邮政编码时 enter image description here

然后就变成了这样。 enter image description here

如何在输入框中允许Zero?

这就是我生成控件的方式

[MinLength(5, ErrorMessage = "{0} Code should have at least {1} character length.")]
[DataType(DataType.PostalCode)]
[StringLength(9)]
public string Zip { get; set; }

1 个答案:

答案 0 :(得分:0)

Here is the AddressModel:

public class AddressModel
{
    [MinLength(5, ErrorMessage = "{0} Code should have at least {1} character length.")]
    [DataType(DataType.PostalCode)]
    [StringLength(9)]
    public string Zip { get; set; }
}

Here are the Controller members:

public class HomeController : Controller
{
    public ActionResult EEIndex()
    {
        AddressModel am = new AddressModel();


        return View(am);
    }

    [HttpPost]
    public ActionResult EEIndex(AddressModel am)
    {
        if (ModelState.IsValid)
        {
            //is valid
            var valid = true;
        }

        return View();
    }

Here is the view:

This will retain the zero.

@model Testy2.Controllers.AddressModel

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>EEIndex</title>
</head>
<body>
    <div>
        @*@using (Html.BeginForm("EEIndex", "Home", new { UserId = "5" }, FormMethod.Post))*@
        @using (Html.BeginForm("EEIndex", "Home", FormMethod.Post))
        {
            @Html.EditorFor(model => model.Zip, new { htmlAttributes = new { @class = "form-control", maxlength = "9", id = "txtZip", placeholder = "Zip Code", aria_invalid = true, type = "text" } })
            @Html.ValidationMessageFor(model => model.Zip, "", new { @class = "validation-danger" })

            <input type="submit" value="Sign In">
        }

    </div>
</body>
</html>