Set selected value in SelectList after instantiati

2019-01-14 10:32发布

Am I right to think that there is no way to set the selected value in the C# class SelectList after it is created? Isn't that a bit silly?

10条回答
倾城 Initia
2楼-- · 2019-01-14 11:09

The "SelectedValue" property is read-only. Setting it in the constructor is the only way.

Tor, the SelectList is an ASP.NET MVC construct used to create a drop-down list. Doing it your way should work too, but the SelectList should do it for you (and not in JS) if done properly.

查看更多
劫难
3楼-- · 2019-01-14 11:10

The SelectList object is readonly after it was created. if you want to select something you better do it like:

<%= Html.DropDownList("clientId", ViewData["clients"] as List<SelectListItem>,)%>

And in the code behind:

    ViewData["clientId"] = "ASD"; //This should be the value of item you want to select
    ViewData["clients"] = clientItemList; //List<SelectListItem>
查看更多
甜甜的少女心
4楼-- · 2019-01-14 11:10

I needed a dropdown in a editable grid myself with preselected dropdown values. Afaik, the selectlist data is provided by the controller to the view, so it is created before the view consumes it. Once the view consumes the SelectList, I hand it over to a custom helper that uses the standard DropDownList helper. So, a fairly light solution imo. Guess it fits in the ASP.Net MVC spirit at the time of writing; when not happy roll your own...

public static string DropDownListEx(this HtmlHelper helper, string name, SelectList selectList, object selectedValue)
{
    return helper.DropDownList(name, new SelectList(selectList.Items, selectList.DataValueField, selectList.DataTextField, selectedValue));
}
查看更多
Luminary・发光体
5楼-- · 2019-01-14 11:13

I agree with @awrigley and others that it is better to load the selected value using the selectlist constructor in the usual case when you have a single dropdownlist. However, sometimes, one needs to set the selected value on the view, such as when you have a page with n identical dropdowns. Since it is not practical to create such collections in the controller and may not even know in advance how many dropdowns you need, a better approach is to create one instance in the viewmodel as a template with no selected value set and then dynamically create the rest dynamically on the view.

I run into this often when iterating through a list of child items on edit views when the children need dropdowns. In such cases, you can do it like this with one line of code:

@Html.DropDownListFor(x => myViewModelFieldName, new SelectList(Model.MyRawSelectListFromMyViewModel.Select(x=> new {x.Value, x.Text}), "Value", "Text", TheValueYouWantToSelect))
查看更多
We Are One
6楼-- · 2019-01-14 11:24

Could do it pretty easy with jQuery;

$(function() {
    $("#myselectlist option[@value='ItemToSelectValue'].attr('selected', 'true');
});
查看更多
再贱就再见
7楼-- · 2019-01-14 11:25

I think you can do this in the controller. If you are going to render a drop down list with name/ID StateCode, then you can set the selected state using this code after the SelectList is created:

ViewData["StateCode"] = "VT";

It seems that the drop down list HTML helper looks for a ViewData item with the same name as the drop down list that's being created.

I don't think I like using this technique, but it does seem to work.

I do agree that the SelectList class is a little weak at the moment. I'd like to be able to create a SelectList and then select a value later by either value or text.

查看更多
登录 后发表回答