I just completed the visualization logic for my form and now I want to get use of the client side validation that asp.net mvc 3 provides. However even though I'm following some examples I can't make it work and I don't know what might be the reason.
Here is my main view :
@model List<DataAccess.MCS_DocumentFields>
@{
ViewBag.Title = "Documents";
}
<div id="drawForm">
@using (Html.BeginForm("RecieveDataFromDocument", "Forms", FormMethod.Post))
{
@Html.ValidationSummary(true)
<table border="1">
<colgroup>
<col span="1" style="width: 10%;" />
<col span="1" style="width: 40%;" />
<col span="1" style="width: 25%;" />
<col span="1" style="width: 25%;" />
</colgroup>
@Html.Partial("_PartialHeader", Model)
@Html.Partial("_PartialDrawing", Model)
@Html.Partial("_PartialBody", Model)
@Html.Partial("_PartialFooter", Model)
</table>
if (ViewBag.Status == 1)
{
<button type="submit">Save</button>
}
else
{
@Html.ActionLink("Back", "Index")
}
}
</div>
Not too much here actually. Most of the logic is in my partials. I use data annotations so I thought that I'll have some client-side validation by default but it seems to not be the case. What I have done is making sure I have
<appSettings>
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
added to my web.config. Also in my view you can see that I've added
@Html.ValidationSummary(true)
not sure if this is the right place for it but it's there. Also in the example that I'm looking from there is :
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
I don't have such <div>
tags and such class names however when I start my application in the viewsource I can see for each input this :
Name comes from DB
<input data-val="true" data-val-required="The FieldValue field is required." name="[4].FieldValue" type="hidden" value="Name comes from DB" />
which I thought is enough for client side validation to take place. But because I did not get any I added in one of my partial views just for test the following :
<div class="editor-label">
@Html.DisplayFor(x => Model[i].QuestionText)
</div>
<div class="editor-field">
@Html.TextBox("datepicker", "", new { @class = "datepicker" })
@Html.ValidationMessageFor(x => Model[i].QuestionText)
</div>
@Html.HiddenFor(x => Model[i].Id)
//...some code...
<div class="editor-field">
@Html.EditorFor(x => Model[i].FieldValue)
@Html.ValidationMessageFor(x => Model[i].FieldValue)
</div>
@Html.HiddenFor(x => Model[i].Id)
//...more code..
But even those two fields doesn't generate error when validation fails. So I guess I'm either missing something or I'm doing something wrong. I doubt if this kind of validation even works this way - with partials?