Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding initial/default value of dropdown (select) list

I'm having a small issue with setting the initial value of a dropdown. The code below is the view model definition and the initialization in $(document).ready. I have an array called sourceMaterialTypes and a selectedSourceMaterialType representing the selected value of that array. I am initializing the view model with values from the (ASP.Net MVC) Model and ViewBag.

var viewModel = {     sourceMaterialTypes :          ko.observableArray(@Html.Raw(Json.Encode(ViewBag.SourceMaterialTypes))),     selectedSourceMaterialType :         ko.observable(@Html.Raw(Json.Encode(Model.SourceMaterialType))),     ingredientTypes :         ko.observableArray(@Html.Raw(Json.Encode(ViewBag.IngredientTypes))),     selectedIngredientType : ko.observable() };  $(document).ready(function () {      ko.applyBindings(viewModel);      viewModel.selectedSourceMaterialType.subscribe(function(newSourceMaterialType) {         $.getJSON("/IngredientType/FindByMaterialType",                   { "id": newSourceMaterialType })             .success(function (data) {                 viewModel.ingredientTypes($.parseJSON(data));             })             .error(function () { alert("error"); });     }); }); 

The following is the definition of my dropdown (select) list with the Knockout binding definition.

<select id="SourceMaterialTypeId"         name="SourceMaterialTypeId"         data-bind="options: sourceMaterialTypes,                    optionsText: 'Name',                    optionsValue : 'Id',                    value: selectedSourceMaterialType"></select> 

This all works fine except for the initially selected value in the source materials dropdown (selectedSourceMaterialType is bound correctly so when the dropdown selection changes its value is correctly updated, it is only the initial selection I am having a problem with), which is always the first item in the sourceMaterialTypes array on my view model.

I would like the initially selected value to be that which is initialized from the (server-side) model as the value of selectedSourceMaterialType view model property.

like image 775
Simon Fox Avatar asked Jul 11 '11 10:07

Simon Fox


People also ask

How do I set the default dropdown value?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do I get the selected value of dropdown?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list.

How do you display a selected value in a drop down list in HTML?

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).


2 Answers

I guess you need to pass the Id only and not the whole object in the selectedSourceMaterialType observable function ->

selectedSourceMaterialType: ko.observable(@Model.SourceMaterialType.Id) 
like image 196
neebz Avatar answered Sep 20 '22 03:09

neebz


The API has the solution for you, you'll just need to add optionsCaption to your select.

<select id="SourceMaterialTypeId"     name="SourceMaterialTypeId"     data-bind="options: sourceMaterialTypes,                optionsText: 'Name',                optionsValue : 'Id',                value: selectedSourceMaterialType,                optionsCaption: 'Please select...'"></select> 
like image 21
Straith Avatar answered Sep 20 '22 03:09

Straith