2012-01-25 31 views
2

リストボックスに40個の結果を含むリストを複数選択することができますが、選択数を特定の数だけに制限したいC#MVCで、私は持っている:ListBoxForで選択可能な項目の最大数を設定する<>

@Html.ListBoxFor(model => model.Location, new SelectList(Model.AllLocations,  Model.Location), new { id = "AllLocations" }) 

コントロールにこの制約を設定する最良の方法は何ですか?

答えて

2

Javascript。 Htmlはそのためのメカニズムを提供していません。このようなもの:

$(document).ready(function() { 

    // Add a click event for options in the list. 
    $('#MyModel_Location option').click(function() { 

     // Check if the parent has reached the limit of selected items. 
     if ($(this).parent().val().length > 5) { 
      // Removed the selected attribute from this option. 
      $(this).removeAttr("selected"); 
     } 
    }); 
}); 
関連する問題