asp.net mvc datannotation验证选项卡明智(每个选项卡的不同消息)

时间:2018-02-02 09:28:46

标签: c# jquery asp.net-mvc data-annotations

这是我的标签生成的语言明智。

         <div class="col-md-6">
            <ul class="nav nav-tabs" id="tabs">
                @foreach (var item in (IEnumerable<SepiaCMS.Models.lang>)ViewBag.Languages)
                {
                    if (i == 0)
                    {
                        <li class="nav active"><a href="#@item.langid" data-toggle="tab"><label><input type="checkbox" checked="checked" name="IsCheck" />&nbsp;@item.langname</label></a></li>
                    }
                    else
                    {
                        <li class="nav"><a href="#@item.langid" data-toggle="tab"><input type="checkbox" name="IsCheck" checked="checked" />&nbsp;@item.langname</a></li>
                    }
                    { i = i + 1; }
                }
            </ul>     

            <div class="tab-content">
                @{int kk = 0; }
                @foreach (var item in (IEnumerable<SepiaCMS.Models.lang>)ViewBag.Languages)
                {
                    if (k == 0)
                    {
                        <div class="tab-pane fade in active" id="@item.langid">
                            <span>Post Title</span><br />
                            @Html.TextBoxFor(m => m.PostDetailVM[kk].Title)<br />
                            @Html.ValidationMessageFor(m => m.PostDetailVM[kk].Title, "", new { @class = "text-danger" })                          
                            <input type="hidden" name="PostDetailVM[@kk].LangId" value="@item.langid" />                               
                        </div>

                    }
                    else
                    {
                        <div class="tab-pane fade" id="@item.langid">
                            <span>Post Title</span><br />
                            @Html.TextBoxFor(m => m.PostDetailVM[kk].Title)<br />
                            @Html.ValidationMessageFor(m => m.PostDetailVM[kk].Title, "", new { @class = "text-danger" })                             
                            <input type="hidden" name="PostDetailVM[@kk].LangId" value="@item.langid" />                                
                        </div>
                    }
                    { k = k + 1; }
                    { kk = kk + 1; }
                }

            </div>
        </div>

这是我的viewmodel

 public class PostTypeCategoryVM
{

    public int PostId { get; set; }      
    public int PostTypeId { get; set; }
    [Required]
    public string PostType { get; set; }
    [Required]
    public DateTime PublishDate { get; set; }
    [Required]
    public DateTime UnPublishDate { get; set; }
    [Required]       
    public List<PostDetailVM> PostDetailVM { get; set; }
}

public class PostDetailVM
{
    [Required]
    public string Title { get; set; }

    public int LangId { get; set; }
    [Required]
    public string Slug {get;set;}  

}

正在做什么

我的数据库中有三种语言可供使用。因此生成了三个选项卡。如果我按下保存按钮而不输入任何内容,则会打印这些验证,如

   The Title is required
   The Title is required
   The Title is required

我想要什么。

我希望验证能够像数据注释一样以语言方式打印。

   The Title for lang1 is required
   The Title for lang2 is required
   The Title for lang3 is required

任何人都可以给我任何想法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用自定义错误消息来执行此操作。而不是:

@Html.ValidationMessageFor(m => m.PostDetailVM[kk].Title, "", new { @class = "text-danger" })

你可以这样做:

@Html.ValidationMessageFor(m => m.PostDetailVM[kk].Title, string.format("The Title for {0} is required", PROPERTY_RETURNS_LANG_ID), new { @class = "text-danger" })