HOW TO BIND DROPDOWN LIST USING KNOCKOUT.JS IN MVC ENTITY FRAMEWORK

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 4152  View(s)
Ratings:
Rate this:
can anyone please help me solve this problem
  1. Ankit Singh

    Post:341

    Points:2389
    Re: How to bind dropdown list using knockout.js in mvc entity framework

    <!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);

    }

    }

      Modified On Oct-15-2015 03:13:19 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!