How to bind dropdown list using knockout.js in mvc entity framework

can anyone please help me solve this problem
Last updated:10/15/2015 3:12:37 AM

1 Answers

Anonymous User
Anonymous User

<!DOCTYPE html>

<html>

<head>

    <meta name='viewport' content='width=device-width, initial-scale=1'>

    <script src='/Scripts/jquery-2.1.4.min.js'></script>

  <script src='/Scripts/knockout-3.3.0.js'></script>

</head>

<body>

<div id='demo'>

<select class='form-control Dropdown' id='brand' data-bind='options:Brands, optionsText:BrandName,optionsValue:'Id',

value:BrandId, optionsCaption: 'All''>

</select>

</div>

<script>

function viewModel() {

    var self = this;

    self.Brands = ko.observableArray();

    self. BrandId = ko.observable();

    $.getJSON('/Home/BrandList ', function (data) {

        self.Brands (data);

    });

 }

$(function () {

    var NewViewModel = new viewModel ();

    ko.applyBindings(NewViewModel, document.getElementById('demo'));

});

</script>

</div>

</body>

</html>

public class HomeController : Controller

{

public JsonResult BrandList ()

{

var data = db.Database.SqlQuery<Manufacturers>(@'SELECT DISTINCT BrandName,Id

 FROM Brand_table').ToList();

return Json(data, JsonRequestBehavior.AllowGet);

}

}

Answer