JQuery Unobtrusive验证获取MVC For循环错误

时间:2014-10-16 09:45:03

标签: jquery model-view-controller

您好我正在尝试在我的MVC应用程序上使用Unobtrusive Validation但是出错了。

引用的Jquery文件

"~/Scripts/libs/jquery-1.9.1.js",
"~/Scripts/libs/jquery.browser.min.js", 
"~/Scripts/libs/bootstrap.min.js", 
"~/Scripts/libs/jquery.validate.min.js", 
"~/Scripts/libs/jquery.validate.unobtrusive.min.js", 
"~/Scripts/libs/jquery.unobtrusive-ajax.min.js",

视图模型

public class UserGroupNameMappingViewModel
{
    public string GeneralName { set; get; }

    [Required(ErrorMessage = "This Field is required")]
    public string MappingName { set; get; }

    public UserGroupNameMappingViewModel() { }
}

客户端表单

@model List<UserGroupNameMappingViewModel>

@using (Html.BeginForm()){

@Html.AntiForgeryToken()

        for (int i = 0; i < Model.Count(); i++)
        {
            <div class="row form-group">

                @Html.DisplayFor(m => Model[i].GeneralName)
                @Html.ValidationMessageFor(m => Model[i].MappingName)
                @Html.HiddenFor(m => Model[i].GeneralName)
                @Html.LabelFor(m => Model[i].MappingName)
                @Html.TextBoxFor(m => Model[i].MappingName)
                <br />
            </div>
        }

        <button type="submit" class="btn btn-success">Update</button>
    }
</div>

我得到的Javascript错误(从Jquery.validate.unobtrusive.min.js抛出)

Uncaught Error: Syntax error, unrecognized expression: label[for='[0].MappingName'], label[for='[0].MappingName'] *, #[0].MappingName-error

请让我知道你的想法,谢谢!!

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案,

每个

@ Html.TextBoxFor(m =&gt; Model [i] .MappingName)

我需要提供一个好的格式化ID。例如

@ Html.TextBoxFor(m =&gt; Model [i] .MappingName,new {id =“Model_MappingName _”+ @i})