c# - Trying to bind data from sql query to view for a dropdown -


my sample code :

model hold list items:

public ilist<listitem> listofrecords { get; set; } 

populating model in controller:

public actionresult index() {     var model = new viewmodel();     model.listofemployees = getallrecordsfromdatabase();     return view(model); } 

my view:

<select data-bind="options: optionvalues, selectedoptions: selectedoptionvalues"></select> 

knockout code populating dropdown:

function donotcallmodel(optionvalues, selectedoptionvalues) { var self = this; self.optionvalues = ko.observablearray(optionvalues);  self.selectedoptionvalues = ko.observable(selectedoptionvalues); return self;  }    @foreach (var item in model)             {                 <text>                 model.contactusers.push(new contactmodel('@item.id',             '@httputility.javascriptstringencode(item.insertrequestedbyemployee)',             '@httputility.javascriptstringencode(item.phonenumber)',             '@httputility.javascriptstringencode(item.emailaddress)',             '@httputility.javascriptstringencode(item.notes)',             '@httputility.javascriptstringencode(item.insertedby)',             '@item.insertdate'               ))             </text>             }             window.model = model;             ko.applybindings(model); 

am not sure how populate data in model dropdown. have done data-binding using knockout input type. first time trying dropdown.

you need several more bindings, assuming you're trying bind complex object drop down.

optionstext - defines text show

optionsvalue - value of selected option should be

value - value of dropdown once option selected (separate observable in viewmodel)

//contract type collection self.contracttypes = ko.observablearray();  //contracttype object self.contracttype = function (data) {     this.contracttype = ko.observable(data.contracttype);     this.contracttypeid = ko.observable(data.contracttypeid); };  //value of selected contract type self.selectedcontracttype = ko.observable();  <select data-bind="options: contracttypes, optionstext: 'contracttype', optionsvalue: 'contracttypeid', value: selectedcontracttype></select> 

your binding stuff multiple options selected @ same time.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -