0
我有主視圖和局部視圖:ASP.NET MVC 4 IEnumerable的<MyModel>客戶端驗證
// // GET:/問題/添加/
public ActionResult Add()
{
return View();
}
//
// Post: /Question/GetAnswerContainers/
[HttpPost]
public ActionResult GetAnswerContainers(int count)
{
ViewBag.AnswerCount = count;
return PartialView();
}
主要觀點是相當簡單的一個:
@using (@Html.BeginForm())
{
@Html.AntiForgeryToken()
<fieldset>
<legend>Add Question Form</legend>
<div id="question_answers">
@Html.LabelFor(m => m.AnswerCount)
@Html.TextBoxFor(m => m.AnswerCount,
new {
@class = "answer_count",
action = @Url.Action("GetAnswerContainers")
})<br/>
@Html.ValidationMessageFor(m => m.AnswerCount)<br/>
<div id="answers"></div>
</div>
<input type="submit" value="Add" />
</fieldset>
}
我也有jQuery的我的文本框:
$(function() {
$('#question_answers input[type="text"].answer_count').keyup(function() {
var answerCount = $(this).val();
if (isNaN(answerCount))
return;
$.ajax({
type: 'POST',
data: { count: $(this).val() },
url: $(this).attr("action"),
timeout: 2000,
async: false,
success: function (data) {
removeAnswers();
addAnswers(data);
$('form').removeData('validator');
$('form').removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse('form');
},
error: null
});
});
});
function addAnswers(data) {
var answersContainer = $('#question_answers #answers');
answersContainer.append($((data)));
}
局部視圖:
@model Quiz.Models.Question.QuestionAnswer
@Html.LabelFor(m => m.AnswerText)
@for (var i = 0; i < ViewBag.AnswerCount; i++)
{
@Html.TextBoxFor(m => m.AnswerText)
@Html.ValidationMessageFor(m => m.AnswerText)
}
型號:
public class QuestionAnswer
{
[Required]
[Display(Name = "Answers")]
public string AnswerText { get; set; }
}
所以我的問題是標準的asp.net客戶端驗證無法與問題答案模型的許多對象句柄,它是取第一個文本框從for
循環,並用它來驗證所有的局部視圖。 是否可以在客戶端正確驗證來自一個模型的許多對象?