我们目前正在使用ASP.NET MVC 4制作一个项目。因为我正在使用select2插件,验证并不是那么容易。 jQuery验证中所需项目的标准消息是“此字段是必需的!”。经过数小时和数小时的搜索,我发现如何更改此消息。下面你可以看到我如何改变它,这是工作(消息得到显示使用@Html.ValidationMessageFor(--property of my ViewModel--)
。这是非常奇怪的工作,因为我不能得到实际的数据注释消息显示,但jQuery消息确实很幸运jQuery验证:根据类别设置更多必需的消息
$.validator.messages = {
required: "@Resources.Question_MinOnePol"
};
这工作,问题是我现在得到每个必填字段相同的消息。我想基于ID或类可能得到不同的自定义消息。
我不知道如何做到这一点?希望你能给我提示如何做到这一点。
其他的:
使用选择列表例选择2:
<select name="PoliticianIds" id="polDrop" multiple style="width: 700px" required class="select2pol required">
@foreach (var par in ((List<Party>)ViewBag.Parties))
{
<optgroup label="@par.Name (@par.FullName)">
@foreach (var pol in ((List<Politician>)par.Politicians))
{
@(ViewBag.CurrPol = pol)
<option value="@pol.UserId">@pol.FirstName @pol.LastName (@pol.Party.Name)</option>
}
</optgroup>
}
</select>
@Html.ValidationMessageFor(model => model.PoliticianIds)
脚本,使之成为选择2列表:
jQuery('#polDrop').select2({
placeholder: "Selecteer een politicus...",
maximumSelectionSize: 5,
formatResult: format2,
formatSelection: format2,
escapeMarkup: function(m) {
return m;
}
});
我的视图模型对于任何有兴趣:
public class CreateQuestionModel
{
public Question Question { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "Question_MinOnePol")]
public List<int> PoliticianIds { get; set; }
public List<int> TopicIds { get; set; }
}
我会很高兴在需要时提供任何额外的细节。