I have a view and a model, the model contains questions and a prop to store result for the particular question. I cannot get it to validate the questions, it always passes the Model validation test on submitting (inside the controller). I checked that jquery, jqueryvalidate and jqueryunobrtusive are both included in that order, I tried moving Summary inside and outside the form, didn't help. It appears after submit as if all is valid and messages are rendered upon rendering of the view.
public class Result
{
[Required]
public int ResultID { get; set; }
[Required]
public string Text { get; set; }
}
public class Question
{
public int Id { get; set; }
public string Text { get; set; }
public Result Result{ get; set; }
}
<div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" });
</div>
@using (Html.BeginForm("Submit", "post", FormMethod.Post, new { id = "Form" }))
{
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
for (int i = 0; i < Model.Count; i++)
{
var item = Model[i];
<div>
<div>
@item.Text
@Html.TextBoxFor(m => m[i].Result.Text, null, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m[i].Result.Text)
@Html.HiddenFor(m => m[1].Result.ResultID )
</div>
</div>
}
}
<div>
<input type="submit" value="Submit"/>
</div>
}
public ActionResult Index()
{
List<Question> Questions = GetAllQuestions();
return View("View", Questions);
}
public ActionResult Submit(List<Question> sQuestions)
{
if (!ModelState.IsValid)
{
return View("Index", );
}