in Education by
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. 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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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)

Related questions

0 votes
    If I want to use the validation framework that you can use with ASP.NET MVC, will the JavaScript ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    This is a Dropdown control where I am binding the data, after bind I am putting the select statement. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am using a user control and I need to call a popup, which asks a question, and I need to send ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have an XML document which basically looks like this: SomeSeller 2535 Now I'm trying to parse the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a viewmodel with an Id property [Required] public int Id { get; set; } But I think this ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I have a viewmodel with an Id property [Required] public int Id { get; set; } But I think this ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I am trying to use MVC model to create "update news" page. News has an image and i am using ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    Story I'm using SQL Server database, and there is a table called aspnet_Membership that it seems like it' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm new to React with Redux and I've been working on a new web application that has some basic ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I'm trying to find an HTML templating solution that will work both on my ASP.NET MVC application ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Knowing that I can version js and css files like Option 1: myJavaScript.js?v=#myVersionNumber# myCSS.css?v= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I have an , and I need to apply a JavaScript call upon it. The JavaScript function should set the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have an , and I need to apply a JavaScript call upon it. The JavaScript function should set the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I'm using Semantic-UI-React in my React/Rails project and trying to use a Form.Select drop down ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm using Semantic-UI-React in my React/Rails project and trying to use a Form.Select drop down ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
...