ValidationMessageFor mvc仅用于十进制

时间:2012-11-13 09:21:06

标签: c# asp.net-mvc entity-framework validation

我有一个带有EF的ASP.NET MVC 4项目,我在一些视野中有一些验证问题。 我注意到ValidationMessageFor仅适用于我的十进制字段

<script type="text/javascript" language="javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" language="javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
@using (Html.BeginForm("Create", "Order", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)

<div class="editor-label">
    @Html.Label("Adresa livrare")
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.adresa, new { maxlength = "80" }) @*string - nvarchar*@
    @Html.ValidationMessageFor(model => model.adresa)
</div>
<div class="editor-label">
    @Html.Label("Curs Euro")
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.curs_euro, new { onkeypress = "return numericOnly(this)", id = "euro", maxlength = 9 }) @*decimal - decimal(10,2)*@
    @Html.ValidationMessageFor(model => model.curs_euro, Resources.Global.strCursEuroV)
</div>

问:为什么验证不适用于字符串字段?

我试图装饰我的模型设计师并添加[必需],但它搞砸了我的所有项目。

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
[Required(ErrorMessage = "validation messeage...")]
public global::System.String adresa

HTML

    <div class="editor-label">
        <label for="Adresa_livrare">Adresa livrare</label>
    </div>
    <div class="editor-field">
        <input id="adresa" maxlength="80" name="adresa" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="adresa" data-valmsg-replace="true"></span>
    </div>
    <div class="editor-label">
        <label for="Curs_Euro">Curs Euro</label>
    </div>
    <div class="editor-field">
        <input data-val="true" data-val-number="The field curs_euro must be a number." data-val-required="The curs_euro field is required." id="euro" maxlength="9" name="curs_euro" onkeypress="return numericOnly(this)" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="curs_euro" data-valmsg-replace="false">Introduceti cursul Euro</span>
    </div>

1 个答案:

答案 0 :(得分:1)

    <div class="editor-label">
        @Html.Label("Adresa livrare")
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.adresa, new { minlength = 1, maxlength = 80 })
        @Html.ValidationMessageFor(model => model.adresa, "Adresa de livrare este    obligatorie")
    </div>//You need to set a minimum number of characters