MVC核心 - asp-validation-for和asp-for正在生成不同的ID

时间:2016-10-17 09:41:13

标签: asp.net-mvc asp.net-core unobtrusive-validation tag-helpers

我正在尝试使用jquery-validation-unobtrusive并且它没有在相邻的跨度中显示错误消息。我发现View Engine正在为Textbox和Error Span标签呈现不同的ID。

有没有办法强制这些ID命名系统呈现相同的ID?

视图模型:

public class RequestJobViewModel
{
    public Models.StudentModel Student { get; set; }
    public Models.JobModel Job { get; set; }

    public decimal StudentPrintingBalance { get; set; }

}

工作模式:

[Required]
public int NoOfOriginalSides { get; set; }

view.cshtml

<div class="form-group form-group-sm required">
     <label class="control-label col-md-2" asp-for="@Model.Job.NoOfOriginalSides">No. of Copies:</label>
     <div class="col-md-10">
          <input type="text" class="form-control text-right" maxlength="3" asp-for="@Model.Job.NoOfOriginalSides" />
          <span asp-validation-for="@Model.Job.NoOfOriginalSides" class="text-danger" />
     </div>
</div>

但是当使用下划线使用不同的ID进行渲染时: Job_NoOfOriginalSidesJob.NoOfOriginalSides

<label class="control-label col-md-2" for="Job_NoOfOriginalSides">No. of Pages:</label>
<div class="col-md-10">
    <input id="Job_NoOfOriginalSides" class="form-control text-right input-validation-error" style="width: 50px;" data-val="true" data-val-required="The NoOfOriginalSides field is required." name="Job.NoOfOriginalSides" value="" type="text">
    <span class="text-danger field-validation-error" data-valmsg-for="Job.NoOfOriginalSides" data-valmsg-replace="true"> </span>
</div>

1 个答案:

答案 0 :(得分:0)

使用name属性而不是Id将错误消息span链接到输入控件。 自那封信&#34;。&#34;在Id中无效,MVC将其替换为任何其他特殊字符以强调