У меня есть RegisterModel, состоящая из 5 моделей SecretQuestionAndAnswerModel
public class RegisterModel
{
public SecretQuestionAndAnswerModel SecretQuestion1 { get; set; }
public SecretQuestionAndAnswerModel SecretQuestion2 { get; set; }
public SecretQuestionAndAnswerModel SecretQuestion3 { get; set; }
public SecretQuestionAndAnswerModel SecretQuestion4 { get; set; }
public SecretQuestionAndAnswerModel SecretQuestion5 { get; set; }
}
и
public class SecretQuestionAndAnswerModel
{
public SecretQuestionTypeModel Type { get; set; }
[Required(ErrorMessage = "Please choose a question.")]
public int Id { get; set; }
public string Question { get; set; }
[Required]
[StringLength(20)]
public string Answer { get; set; }
[Display(Name = "Select Question")]
public IEnumerable<SelectListItem> DefaultQuestions { get; set; }
}
В моем основном файле Register.cshtml я включаю каждый вопрос следующим образом:
@Html.Partial("_QuestionAndAnswer", Model.SecretQuestion1, new ViewDataDictionary { { "Index", 1 }})
и эта часть выглядит следующим образом:
<div class="input">
@{
@Html.DropDownListFor(m => Model.Id, Model.DefaultQuestions, "(Please choose one)")
@Html.ValidationMessageFor(m => Model.Id)
@Html.TextBoxFor(m => m.Answer)
@Html.ValidationMessageFor(m => m.Answer)
}
</div>
Но при проверке все 5 вопросов ведут себя как один : другими словами, если я выбираю вопрос и набираю ответдля 1-го секретного вопроса все они проходят проверку, и наоборот ...
Кроме того, при отправке в регистр HttpPost
public ActionResult Register(RegisterModel model) { ...
модель.SecretQuestion1 всегда равна нулю.Все они есть.Как вы делаете этот вид вложенной модели привязки?Я думал, что это будет работать нормально.