jquery.validate.unobtrusive.js 不显示消息

jquery.validate.unobtrusive.js doesn't show Messages

我使用 jquery.validate.unobtrusive.js 进行验证,但 不显示警告消息 ,但是验证效果很好。

我该如何解决? :)

以及如何更改消息语言?!

<script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

形式

   <!-- beging form-->

            <form asp-action="AddUrl" method="post" id="formLink">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    
                <div class="form-group row">
                    <div class="col-lg-4 col-md-9 col-sm-12">
                        <select class="form-control" data-live-search="true" name="lessonsDD" id="lessonsDD" asp-for="LessonId">
                            <option value="">درس را انتخاب کنید</option>
                        </select>
                        <span asp-validation-for="LessonId" class="text-danger"></span>
                    </div>

                    <div class="col-lg-4 col-md-9 col-sm-12">
                        <select class="form-control" data-live-search="true" name="chapterDD" id="chapterDD" asp-for="ChapterId">
                            <option value="">فصل را انتخاب کنید</option>
                        </select>
                        <span asp-validation-for="ChapterId" class="text-danger"></span>
                    </div>
                    
                </div>

                <div class="form-group">
                    <label asp-for="Url" class="control-label"></label>
                    <input type="url" asp-for="Url" class="form-control" name="url" />
                    <span asp-validation-for="Url" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <label asp-for="Description" class="control-label"></label>
                    <textarea asp-for="Description" class="form-control"></textarea>
                    <span asp-validation-for="Description" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="افزودن" class="btn btn-primary" />
                </div>
            </form>

i used jquery.validate.unobtrusive.js for validation but it does not show Warn messages

您需要删除输入的名称属性。

And how I can change the messages language ?!

您可以尝试configure data annotation localization使用共享资源文件:

 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
                .AddViewLocalization(o => o.ResourcesPath = "Resources")
                .AddDataAnnotationsLocalization(o =>
                {
                    var type = typeof(SharedResources);
                    var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
                    var factory = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
                    var localizer = factory.Create("SharedResources", assemblyName.Name);
                    o.DataAnnotationLocalizerProvider = (t, f) => localizer;

                }); 

共享资源的结构:

SharedResources.cs:

public class SharedResources
    {
    }

SharedResources.fr.resx: 型号:

public class LocalModel
    {
        public string LessonId { get; set; }
        public string ChapterId { get; set; }
        [Required(ErrorMessage = "The {0} field is required.")]
        public string Url { get; set; }
        [Required(ErrorMessage = "The {0} field is required.")]
        public string Description { get; set; }
    }

结果: