简体   繁体   English

SelectList问题(选定值)

[英]SelectList issue (Selected value)

I have several DDL in a Modal Bootstrap (5) and those DDL binding with a Viewbag from the Controller, Like this: 我在Modal Bootstrap(5)中有几个DDL,并且这些DDL与Controller中的Viewbag绑定,如下所示:

(Controller) (控制器)

ViewBag.StateID = new SelectList(db.State, "StateID", "StateDesc", events.StateID);
ViewBag.CountryID = new SelectList(db.Country, "CountryID", "CountryDesc",events.CountryID);
ViewBag.HotelID = new SelectList(db.Hotel, "HotelID", "HotelDesc", events.HotelID);
ViewBag.AirportID = new SelectList(db.Airport, "AirportID", "AirportDesc", events.AirportID);
ViewBag.AirlineID = new SelectList(db.Airline, "AirlineID ", "AirlineDesc", events.AirlineID);

My view work perfectly fine and populate the DDL and show the selected item if my code declaration is this: 如果我的代码声明是这样,我的视图就可以很好地工作并填充DDL并显示所选的项目:

(View) (视图)

@Html.DropDownList("AirlineID", String.Empty)

(Javascript) (JavaScript)

<script type="text/javascript">
    $('#AirlineID').attr('class', 'chosen-select form-control required');
    $('#AirportID').attr('class', 'chosen-select form-control required');
    $('#StateID').attr('class', 'chosen-select form-control required');
    $('#CountryID').attr('class', 'chosen-select form-control required');
    $('#HotelID').attr('class', 'chosen-select form-control required');
</script>

but, If my code is declared from this way, the Selected item don't appear or Show: 但是,如果以这种方式声明了我的代码,则“选择”项不会出现或不显示:

@Html.DropDownList("AirportID", (IEnumerable<SelectListItem>)ViewBag.AirportID, String.Empty, new { @class = "chosen-select form-control required" })

I use chosen-select class 我使用选择选择类

Why is that? 这是为什么? Missing Declaration or Code? 缺少声明或代码? Wrong code? 错误代码?

Thanks you 谢谢

You cannot use the same name for both the property your binding to and the SelectList 您不能对绑定的属性和SelectList使用相同的名称

@Html.DropDownList("CountryID", (IEnumerable<SelectListItem>)ViewBag.CountryID, ...)

means you binding to a property named CountryID but in you case, CountryID is a SelectList not a value type (and a <select> element can only be bound to a value type. 表示您绑定到名为CountryID的属性,但在这种情况下, CountryID是一个SelectList而不是值类型( <select>元素只能绑定到值类型。

Internally the method generates a collection of <option> elements and sets the value attribute and text. 该方法在内部生成<option>元素的集合并设置value属性和文本。 As it does so, it checks the value of the property your binding to. 这样做时,它将检查绑定到的属性的值。 If the value of the property matches the value of the option then the selected="selected" attribute is rendered. 如果属性的值与选项的值匹配,那么将显示selected="selected"属性。 In your case CountryID is not a int value which matches one of the StateID values your generating in the options so selected="selected" is never set on any option because the value of CountryID is "System.Web.Mvc.SelectList" (not "1" or "2" etc) 在你的情况CountryID不是int相匹配的一个值StateID值在这样的选项中生成selected="selected"决不会在任何选项设置,因为值CountryID"System.Web.Mvc.SelectList" (未"1""2"等)

Setting the last parameter of the SelectList constructor is simply ignored when your binding to a property. 当您绑定到属性时,设置SelectList构造函数的最后一个参数将被忽略。

You can make this work by specifying null as the second parameter, meaning the helper falls back to using the SelectList as the first parameter. 您可以通过将null指定为第二个参数来完成这项工作,这意味着帮助程序会退回到使用SelectList作为第一个参数。

@Html.DropDownList("CountryID", null, String.Empty, new { @class = "chosen-select form-control required" })

However the recommended approach is to use a view model, for example 但是,建议的方法是使用视图模型,例如

public class MyViewModel
{
  [Display(Name = "Country")]
  [Required(ErrorMessage = "Please select a country")]
  public int CountryID { get; set; }
  public SelectList CountryList { get; set; }
}

and in the GET method, initialize an instance or the view model, map your data model properties to it and assign the SelectList properties. 在GET方法中,初始化实例或视图模型,将数据模型属性映射到该实例或视图,并分配SelectList属性。 Then in the view, use the strongly typed html helpers to bind to your model properties. 然后在视图中,使用强类型的html帮助器绑定到您的模型属性。

@Html.LabelFor(m => m.CountryID)
@Html.DropDownListFor(m => m.CountryID, Model.CountryList, "-Please select-", new { @class="..." })
@Html.ValidationMessageFor(m => m.CountryID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM