I have the following view that i need to display an error message when user tries to submit with items selected greater than 5. I could do this on the controller side but could i use jquery in the view to do this? I think jquery would be lot easier and faster.
Here is my code:
@using (Html.BeginForm())
{
<h1>Manage Testimonials</h1>
<p><b>Please select up to 5 testimonials you want to include for your sales flyers. A maximum of only 5 testimonials is allowed.</b></p>
<div>
<table width="100%" cellpadding="0" cellspacing="0" rules="all">
<thead>
<tr>
<td align="center" style="padding: 2px 0 2px 2px;">Select</td>
<td align="center" style="padding: 2px 0 2px 2px;">First Name</td>
<td align="center" style="padding: 2px 0 2px 2px;">Last Name</td>
<td align="center" style="padding: 2px 0 2px 2px;">Testimonial</td>
</tr>
</thead>
@{ var i = 0;
}
@foreach (var testimonials in Model.Testimonials)
{
<tr>
<td style="padding: 2px 0 2px 2px;">
@Html.CheckBox("Testimonials[" + i.ToString() + "].DisplayTestimonials", testimonials.DisplayTestimonials.Value, new { @class = "chkItems" })
@Html.Hidden("Testimonials[" + i.ToString() + "].ResponseId", testimonials.ResponseId.ToString())
</td>
<td style="padding: 2px 0 2px 2px;">@Html.Label(testimonials.FirstName)</td>
<td style="padding: 2px 0 2px 2px;">@Html.Label(testimonials.LastName)</td>
<td style="padding: 2px 0 2px 2px;">@Html.Label("Testimonials[" + i.ToString() + "].Question5Answer", testimonials.Question5Answer.ToString())</td>
</tr>
i++;
}
</table>
<p>
<input type="submit" id="Submit" value="Save" class="PremierSubmitButton" /></p>
</div>
}
Don’t rely on javascript, always validate data on server side. Maybe user disabled javascript in browser. use Ajax form reqest if you need ajax for this.
Stay DRY 😉