Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Data Validation - Highlight Table Row vs. TextBoxes
    text
    copied!<p>On an ASP.NET MVC View, I have a couple of checkboxes, one for email address and one for phone. I want to make sure that at least one is checked (both can be checked, so a radio button is not ideal) and if neither are, highlight the row with a red border just like a textbox is with the validation functionality...</p> <p>I have other fields that are getting validated correctly and the CSS is changing when there is an issue on the textboxes and textareas accordingly. The code below displays the message informing the user they must specify a contact preference, but does not highlight the row as having an issue...</p> <p><strong>SCREEN SHOT</strong> <a href="http://i41.tinypic.com/2hcgfew.jpg" rel="nofollow noreferrer">alt text http://i41.tinypic.com/2hcgfew.jpg</a></p> <p><strong>VIEW</strong></p> <pre><code>&lt;table width="100%"&gt; &lt;tr&gt; &lt;td&gt; &lt;label&gt; How would you like us to contact you? &lt;/label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr id="pref_row"&gt; &lt;td&gt; &lt;span class="bold-text"&gt;Email: &lt;/span&gt;&amp;nbsp; &lt;%=Html.CheckBox("EmailDesired")%&gt; &amp;nbsp; &amp;nbsp; &lt;span class="bold-text"&gt;Phone: &lt;/span&gt;&amp;nbsp; &lt;%=Html.CheckBox("PhoneDesired")%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p><strong>CONTROLLER</strong></p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(ContactUs contactus) { ContactUsService svc = new ContactUsService(); // Validation if (!contactus.EmailDesired &amp;&amp; !contactus.PhoneDesired) ViewData.ModelState.AddModelError("pref_row", "Please specify a contact preference (phone and/or email)!"); if (ViewData.ModelState.IsValid) { MessageModel msg = svc.SendRequest(contactus); return RedirectToAction("Index", msg); } else { return View(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload