0

我有一个带有 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>

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

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

[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>
4

1 回答 1

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 
于 2012-11-13T15:09:55.247 回答