Porter
Porter

Reputation: 313

How to set default selected of ComboBox when binding to a model?

    @Html.DevExpress().ComboBoxFor(m => m.HealthCareWorker, settings =>
    {
        settings.Width = Unit.Percentage(25);
        settings.Properties.Caption = "HealthCareWorker";
        settings.Properties.Items.Add("Yes", 1);
        settings.Properties.Items.Add("No", 2);
        settings.Properties.Items.Add("Unknown", 3);
    }).GetHtml()

brand new to devExpress and don't have all day to keep trying solutions that don't work. how do I set a default selected with this setup the quickest?

settings.SelectedIndex = 2; //NOTE doesn't work
settings.SelectedIndex = Model.HealthCareWorker; //NOTE set HealthCareWorker default to 2, but doesn't work as says Model is undefined

also followed patterns outlined in: https://supportcenter.devexpress.com/ticket/details/q530713/how-to-bind-combobox-with-list-and-bind-to-a-record-by-value but no success.

Thanks!

Upvotes: 0

Views: 398

Answers (1)

Mikhail
Mikhail

Reputation: 9300

If you need to set the (default) ComboBox Value according to the Model, pass the Model instance with the default property (in your case - "HealthCareWorker") value to the related View:

Your Model:

public class MyModel {
    ...
    public int HealthCareWorker { get; set; }
}

Your Controller:

public ActionResult YourActionMethod() {
    ...
    var emptyModel = new MyModel { HealthCareWorker = 2 };
    return View(emptyModel);
}

Your View (with ComboBox):

@Html.DevExpress().ComboBoxFor(m => m.HealthCareWorker, settings => {
    ...
    settings.Properties.Items.Add(...);
}).GetHtml()

MVC Data Editors - Model Binding and Editing

Upvotes: 1

Related Questions