0
votes

I am new in knockout.i want that when i click on edit button based on the value the dropdown should be selected.

Here is My Full Code...I am trying to Edit the Record...

Here is My HTML Code

Here is my JS code...

function item(id, name) { this.id = ko.observable(id);

    this.name1 = ko.observable(name);

}

function CompanyViewModel() {

var self = this; var Sort = "desc";

    self.id = ko.observable("");
    self.name = ko.observable().extend

({ required: true }); self.bloombergcode = ko.observable().extend({ required: true });

    self.Id = ko.observable();

    self.sector1 = [new item(1, "Banking"), new item(2, "Non-Banking")];

    self.country = ko.observableArray([]);

    self.sectorid = ko.observable().extend({ required: true });

    self.isincode = ko.observable();
enter code here
    self.address = ko.observable();

    this.validationModel = ko.validatedObservable({
        name: self.name,
        bloombergcode: self.bloombergcode,
        sectorid: self.sectorid

    });



    //        self.items = ko.observableArray([]);
    var company =
{
    id: self.id,
    name: self.name,
    bloombergcode: self.bloombergcode,
    sectorid: self.sectorid,
    isincode: self.isincode,
    sector1: self.sector1,
    address: self.address
};

    self.company = ko.observable();
    self.companies = ko.observableArray();


    $.ajax({
        url: '@Url.Action("CompanyId", "Company")',
        cache: false,
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        data: {},
        success: function (data) {

            //self.companies(data);
            self.company(data); 

            self.id(data.id);

            self.name(data.name);

            self.sectorid = ko.observable(new item(data.sectorid, ""));

            self.isincode(data.isincode);

            self.address(data.address);

            self.sectorid(self.sector1[1].id);

           // self.sectorid(self.sector1[1]);


            self.bloombergcode(data.bloombergcode)
        }
    });

}

var viewModel=new CompanyViewModel(); ko.applyBindings(viewModel);

1
Any progress with this issue? Have you tried to implement my solution? Let me know if you have any further questions.Vinney Kelly
Your HTML code is missing and your code is really hard to read because some of the code is outside of the code regions. Please have considerations for those of us who are trying to help you solve your problems. A little more effort would be appreciated.Vinney Kelly
My Html code is complete...and only the one function is outsid.Please tell me what kind of effort you expected from me...?user3217843
Please some one help me...user3217843

1 Answers

0
votes

Since you supplied the optionsValue parameter, you need to set sectored to the id value. In other words:

self.sectorid(self.sector[1].id());

Also, your binding statement is incorrect. You're missing quotes around id on the optionsValue parameter. Such as:

<select id="sector" data-bind="options:sector1 , validationElement: sectorid, valueUpdate: 'afterkeydown', optionsValue:'id', optionsCaption: '........Select........', value: $root.sectorid, optionsText: 'name1' "></select>        

Hope that helps!

P.S. For clarification, without supplying the optionsValue, knockout will use the actual object as the value. In that case, the code you had originally, self.sectorid(self.sector[1]) would have worked as expected.