1

入力ボタンが[非表示]、非表示になっています。非表示のボタンはテキストボックスを表示し、非表示のボタンはテキストボックスを非表示にします。表示されるボックスに対してのみ検証を有効にし、クライアントには表示されないボックスに対する検証を無効にします。現在、私はすべての箱に検証しています、ポストアクションもjqueryの経由隠されているテキストボックスを検証し、以下のコードです:mvcの特定の非表示のテキストボックスの有効/無効の確認2

ビュー

<script type="text/javascript"> 

$(document).ready(function() { 
    var $startdates = $('#startDates'); 
    var $endDates = $('#endDates'); 
    var $showEvents = $('#showEvents'); 
    $startdates.hide(); 
    $endDates.hide(); 
    $showEvents.hide(); 

    $('#all').click(function() { 
     $startdates.show(); 
     $endDates.show(); 
     $('#showEvents').show(); 
     $('#eventdids').hide(); 
     $(this).hide(); 
     return false; 

    }); 

    $('#showEvents').click(function() { 
     $startdates.hide(); 
     $endDates.hide(); 

     $('#eventdids').show(); 
     $('#all').show(); 
     $(this).hide(); 
     return false; 

    }); 
}); 
</script> 
<tr id="startDates"> 
     <td> 
     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.StartDate) %> 
     </div> 
     </td> 
     <td> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.StartDate) %> 
      <%: Html.ValidationMessageFor(model => model.StartDate) %> 
     </div> 
     </td> 
     </tr> 

     <tr id="endDates"> 
     <td> 
     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.EndDate) %> 
     </div> 
     </td> 
     <td> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.EndDate) %> 
      <%: Html.ValidationMessageFor(model => model.EndDate) %> 
     </div> 
     </td> 
     </tr> 


     <tr id="eventdids"> 
     <td> 
     <label>Events</label> 
     </td> 
     <td> 
     <% foreach (var item in (SelectList)ViewData["events"]) { %> 
       <input type="checkbox" name="Name" value="<%=item.Value %>" /> 
        <label for="<%=item.Value%>"><%=item.Text%></label> 
        <br /> 

     <% } %> 

     </td> 
     <td><input type="button" name="Select" id="all" style="width:auto" value="Hide" /></td> 


     </tr> 

     </table> 
     <input type="button" name="show" id="showEvents" style="width:auto" value="Show" /> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 

コントローラー:

[HttpPost] 
    public ActionResult Create(FormCollection collection, int[] Name) 
    { 

     IVoucherRepository voucherResp = new VoucherRepository(); 
      string empty = ""; 
      if (Name != null) 
      { 
       for (int i = 0; i < Name.Length; i++) 
       { 
        empty += Convert.ToString(Name[i]) + ","; 
       } 

      } 
      else 
      { 
       ModelState.AddModelError("Venue", "Required"); 

      } 
      if (Convert.ToBoolean(collection["pound"].ToLower().StartsWith("false")) && Convert.ToBoolean(collection["percentage"].ToLower().StartsWith("false"))) 
      { 
       ModelState.AddModelError("","Please Select £ or % for discount"); 
      } 
      if (collection["UsageQtyAvailable"] == null) { ModelState.AddModelError("UsageQtyLeft", "Required "); } 

      Voucher voucher = new Voucher(); 
      if (TryUpdateModel(voucher) && ModelState.IsValid) 
      { 
       voucher.Status = true; 
       voucher.DateAdded = DateTime.Now; 
       voucher.DateModified = DateTime.Now; 
       if(Convert.ToBoolean(collection["pound"].ToLower().StartsWith("true"))) 
       { 
       voucher.DiscountType = 1; 
       } 
       else if (Convert.ToBoolean(collection["percentage"].ToLower().StartsWith("true"))) 
       { 
        voucher.DiscountType = 2; 
       } 
       voucher.VoucherType = 1; //Discount Code 
       voucher.UsageQtyLeft = Convert.ToInt32(collection["UsageQtyAvailable"]); 
       string removeComma = empty.Remove(empty.Length - 1,1); 
       voucher.EventIDs = removeComma; 
       voucherResp.Add(voucher); 
       voucherResp.Save(); 
       return RedirectToAction("Index"); 
      } 

      ITrackdayRepository trackdayResp = new TrackdayRepository(); 
      IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist(); 

      ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID", "Name"); 
      return View(); 

    } 

答えて

2

ますテキストボックスが表示されていない場合はjqueryでチェックし、フォームを送信する前にテキストボックスを削除してから送信されたフォーム要素を検証するだけです。

... 
<input type="submit" value="Create" id="create" /> 
... 

$("#create").click(function() { 
     $('#eventdids:hidden').remove(); 
     $('#all:hidden').remove(); 
    $("#formID").submit(); 
}); 
2

ます。また、この小さなプラグインをフォーム内のすべての隠されたフィールドに対して検証を削除することができます。

(function($) { 
    $.fn.refreshValidator = function() { 
     var form = this; 

     // Get validation settings object 
     var settings = form.validate().settings; 

     // Remove validation for hidden elements 
     $(this).find(':hidden').each(function(){ 
      var id = $(this).attr('id'); 
      delete settings.rules[id]; 
      delete settings.messages[id]; 
     }); 
    }; 
})(jQuery); 

あなたはこのようにそれを呼び出すことができます。

$('#yourFormId').refreshValidator(); 

それは、この方法より、再利用可能です。

関連する問題