如何设置下拉列表

时间:2014-10-30 07:27:03

标签: c# asp.net-mvc-3

我想在值匹配时选择下拉列表,但总是最后一个选项获得选择EUR,请帮我如何执行此操作,这是我的下拉列表代码...

           <select class="select" name="TicketCurrency">
                <option selected="@(ticketDetails != null ? ticketDetails.TicketCurrency == "INR" ? "selected" : "" : "")">
                    INR</option>
                <option selected="@(ticketDetails != null ? ticketDetails.TicketCurrency == "USD" ? "selected" : "" : "")">
                    USD</option>
                <option selected="@(ticketDetails != null ? ticketDetails.TicketCurrency == "EUR" ? "selected" : "" : "")">
                    EUR</option>
            </select>

3 个答案:

答案 0 :(得分:2)

@Stephen是正确的,selected or selected="" or selected="selected"所有人都可以选择,你可以看到最后一个被选中。

基本上,你搞乱了HTML代码!如果您正在使用MVC,为什么不充分利用HTML帮助程序类?以下是您在MVC中重写的示例: -

@Html.DropDownListFor(x => x.TicketCurrency,
                            new List<SelectListItem> { 
                                new SelectListItem { Text = "INR", Value = "INR"},
                                new SelectListItem { Text = "USD", Value = "USD"},
                                new SelectListItem { Text = "INR", Value = "EUR"}
                        }, "Select Currency")

这将自动选择其值在TicketCurrency属性中的选项。也, 我建议你在Model中为下拉列表构建一个类型并在此处绑定它,而不是在View中进行硬编码。

答案 1 :(得分:0)

试试这个

//简单的drowndown

<select class="select" name="TicketCurrency">
                    <option>
                        INR</option>
                    <option>
                        USD</option>
                    <option>
                        EUR</option>
                </select>

//然后在jquery

$(document).ready(function(){
    if(ticketDetails != null){
      $('select').val(@ticketDetails.TicketCurrency);
    }
}):

示例演示小提琴http://jsfiddle.net/Xxk3F/2019/

答案 2 :(得分:0)

试试这个

<select class="btn btn-default" data-original-title="" title="" id="@linktxt">
                                        <option value="1" @if (dish.Quantity == 1) { <text> selected="selected" </text>       }>1</option>
                                        <option value="2" @if (dish.Quantity == 2) { <text> selected="selected" </text>       }>2</option>
                                        <option value="3" @if (dish.Quantity == 3) { <text> selected="selected" </text>        }>3</option>
                                        <option value="4" @if (dish.Quantity == 4) { <text> selected="selected" </text>        }>4</option>
                                        <option value="5" @if (dish.Quantity == 5) { <text> selected="selected" </text>        }>5</option>
                                        <option value="6" @if (dish.Quantity == 6) { <text> selected="selected" </text>        }>6</option>
                                        <option value="7" @if (dish.Quantity == 7) { <text> selected="selected" </text>        }>7</option>
                                        <option value="8" @if (dish.Quantity == 8) { <text> selected="selected" </text>        }>8</option>
                                        <option value="9" @if (dish.Quantity == 9) { <text> selected="selected" </text>        }>9</option>
                                        <option value="10" @if (dish.Quantity == 10) { <text> selected="selected" </text>        }>10</option>
                                        <option value="11" @if (dish.Quantity == 11) { <text> selected="selected" </text>        }>11</option>
                                        <option value="12" @if (dish.Quantity == 12) { <text> selected="selected" </text>        }>12</option>
                                        <option value="13" @if (dish.Quantity == 13) { <text> selected="selected" </text>         }>13</option>
                                        <option value="14" @if (dish.Quantity == 14) { <text> selected="selected" </text>         }>14</option>
                                        <option value="15" @if (dish.Quantity == 15) { <text> selected="selected" </text>         }>15</option>
                                        <option value="16" @if (dish.Quantity == 16) { <text> selected="selected" </text>         }>16</option>
                                        <option value="17" @if (dish.Quantity == 17) { <text> selected="selected" </text>         }>17</option>
                                        <option value="18" @if (dish.Quantity == 18) { <text> selected="selected" </text>         }>18</option>
                                        <option value="19" @if (dish.Quantity == 19) { <text> selected="selected" </text>         }>19</option>
                                        <option value="20" @if (dish.Quantity == 20) { <text> selected="selected" </text>         }>20</option>
                                    </select>