2009-06-09 19 views
4

ASP.NET MVCビューでは、メールアドレス用と電話用の2つのチェックボックスがあります。私は、少なくとも1つがチェックされていることを確認したい(どちらもチェックすることができるので、ラジオボタンは理想的ではない)。もしそうでなければ、テキストボックスのような赤い枠線をハイライト表示する...ASP.NET MVCデータ検証 - テーブル行とテキストボックスをハイライト表示

私は正しく検証されている他のフィールドを持っており、それに応じてテキストボックスやテキストエリアに問題があるとCSSが変化しています。以下のコードは...それらが接触選好を指定する必要があり、ユーザに知らせるメッセージを表示するが、問題を有するものとして行を強調表示しない

スクリーンショット alt text http://i41.tinypic.com/2hcgfew.jpg

VIEW

<table width="100%"> 
    <tr> 
     <td> 
      <label> 
       How would you like us to contact you? 
      </label> 
     </td> 
    </tr> 
    <tr id="pref_row"> 
     <td> 
      <span class="bold-text">Email: </span>&nbsp; 
      <%=Html.CheckBox("EmailDesired")%> 
      &nbsp; &nbsp; <span class="bold-text">Phone: </span>&nbsp; 
      <%=Html.CheckBox("PhoneDesired")%> 
     </td> 
    </tr> 
</table> 

CONTROLLER

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Index(ContactUs contactus) 
{ 
    ContactUsService svc = new ContactUsService(); 
    // Validation 
    if (!contactus.EmailDesired && !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(); 
    } 
} 
+1

何のラジオボタンについては? –

+0

Daniel - 1つをチェックする必要がありますが、両方をチェックすることができます。ラジオボタンは、一般的にAとBのどちらかであり、AとBの両方ではありません。 – RSolberg

答えて

4

HtmlHelper自体がレンダリングされるとき、ModelState辞書にヘルパーと同じキーを持つ項目があるかどうかがチェックされます。そうであれば、コントロールはcssファイルで定義されている "input-validation-error"と等しい属性クラスでレンダリングされます。
したがって、スタイルはレンダリングされた入力コントロールにのみ適用されます。

これは私のソリューションです:

<table width="100%"> 
    <tr> 
     <td> 
      <label> 
       How would you like us to contact you? 
      </label> 
     </td> 
    </tr> 
    <tr class="<%=ViewData.ModelState["pref_row"]!= null ? "input-validation-error":"" %>"> 
     <td> 
      <span class="bold-text">Email: </span> 
      <%=Html.CheckBox("EmailDesired")%> 
       <span class="bold-text">Phone: </span> 
      <%=Html.CheckBox("PhoneDesired")%> 
     </td> 
    </tr> 
</table> 
+0

恐ろしい!私はこれを試してみる... – RSolberg

関連する問題