2016-08-03 15 views
0

ボタンとドロップダウンが動的に生成されています。私はページの読み込み時にドロップダウンのIDにアクセスしたいが、自動的に生成されたIDは最初のドロップダウンのIDだけを与える(スクリプトを見てください)。そして、あなたはどのように適切にasp.net mvcの複数のコントロールの自動生成IDを取得する方法は?

<div class="row"> 
<div class="col-md-9"> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots) 
{ 
if (slot.day == "Monday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
}} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots) 
{if (slot.day == "Tuesday") 
{<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal"data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
} 
} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots) 
{if (slot.day == "Wednesday") 
{ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
} 
} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Thursday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button></div> 
}} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Friday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
}} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Saturday") { 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button></div> 
} 
} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Sunday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button></div>}} 
</div> 
</div>   
<div class="col-md-3"> 
<select data-id="@data.id" id="[email protected]" class="temp"> 
@foreach (var d in ad_list){ 
<option value="@d.id">@d.day_name</option>} 
</select> 
</div> 

<script> 
var today_number = new Date().getDay(); 
var today = get_all(today_number) 
$('select>option:eq(' + today_number + ')').attr('selected', true); 
var id = $('.temp').attr('data-id'); 


</script> 
+0

を見て、すべてのドロップダウンには、同じIDを持っていますか? –

+0

私はそれらに異なるIDを与えるためにdata-idを使用しています。 ページの読み込みにアクセスできない – Zawar

答えて

0

<select data-id="@data.id" id="[email protected]" class="temp">文は、foreachループの外にあるコードをインデントを教えてくださいすることができます。あなたが複数のドロップダウンを生成する場合は以下のように、そして、foreachループ内でselect文を置く:あなたは同上のが生成されているを見てする必要があります

@foreach (var d in ad_list) 
{ 
    <select data-id="@data.id" id="[email protected]" class="temp"> 
     <option value="@d.id">@d.day_name</option> 
    </select> 
} 
-1

現時点でページが同じIDのその意志がたくさんありますあなたの問題を引き起こします。

しかし、ddlは1つのみで、動的に生成されたIDは必要ありません。それはあなたのループの外です。

あなたは動的にIDを生成する必要がある場合here

関連する問題