2016-11-23 88 views
0

是否可以在cshtml 中顯示普通html下拉列表的模型,還是應該單獨使用ajax?什麼是最好的選擇? 將模型顯示到非剃鬚刀下拉列表

  @foreach (var item in Model) 
 
      { 
 
       <tr>    
 
        <td>@Html.DisplayFor(modelItem => item.ReservationId)</td> 
 
        <td>@Html.DisplayFor(modelItem => item.ReservationStartDate)</td> 
 
        <td>@Html.DisplayFor(modelItem => item.ReservationEndDate)</td> 
 
        <td>@Html.DisplayFor(modelItem => item.ReservationAdult)</td> 
 
        <td>@Html.DisplayFor(modelItem => item.ReservationChildren)</td> 
 
        <td>@Html.DisplayFor(modelItem => item.ReservationRoomDesc)</td> 
 
        <td>@Html.DisplayFor(modelItem => item.ReservationRoomType)</td> 
 
        <td><select id="hotelroom" name="cs3" class="cs3 form-control input-small"></select></td> 
 
        <td>@Html.DisplayFor(modelItem => item.ReservationRoomStatus)</td>  
 
        <td><input type="button" id="editTable"></td> 
 
       </tr> 
 
      }

回答

0

您可以創建一個下拉列表,像這樣:

@Html.DropDownListFor(
    modelItem => modelItem.AvailableHotelRoomOptions, 
    new SelectList(Model.AvailableHotelRoomOptions.Select(@r => @r.OptionName)), 
    new { @id = "hotelroom", @class = "cs3 form-control input-small" } 
) 

或者這樣:

@Html.DropDownListFor(
    modelItem => modelItem.AvailableHotelRoomOptions, 
    new SelectList(Model.AvailableHotelRoomOptions, "OptionId", "OptionValue"), 
    new { @id = "hotelroom", @class = "cs3 form-control input-small" } 
) 
0

您可以使用剃刀呈現<option>標籤像任何其他標記。

我假設你有一個IEnumerable<SelectListItem> AvailableHotelRoomOptions以及你的模型中的潛在選項。

<td> 
    <select id="hotelroom" name="cs3" class="cs3 form-control input-small"> 
    @foreach(var selectListItem in Model.AvailableHotelRoomOptions) { 
     @{ string selectedAttr = selectListItem.Selected ? "selected = \"selected\"" : string.Empty; } 
     <option value="@selectListItem.Value" @selectedAttr> 
      @selectListItem.Text 
     </option> 
    } 
    </select> 
</td> 

PS:確保<select>匹配的ID和Name屬性由MVC模型綁定器預期的命名慣例,因此它可以被綁定回來了!