We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Setting DropDownList in script by Guid value

Thread ID:

Created:

Updated:

Platform:

Replies:

143461 Mar 20,2019 09:02 PM UTC Mar 25,2019 07:07 AM UTC ASP.NET Core - EJ 2 3
loading
Tags: Dropdown List
Michael Lambert
Asked On March 20, 2019 09:02 PM UTC

I'm trying to set the selected item in a ejs-dropdownlist by setting its value in script in a razor app.  The value is a guid.

Setting the dropdownlist up:

        public IEnumerable Statuses = null;

        public void OnGet()
        {
            Statuses = res.Status.OrderBy(a=>a.Sort).Select(a => new { text = a.NameShort, value = a.StatusId }).ToList();  //StatusId is a guid
        }

CSHTML--------
        <div class="col-md-3">
            <ejs-dropdownlist id="ddlOfcStatus" dataSource="@Model.Statuses" placeholder="Select Status" />
     
That all works...


This is the change function of another listbox
    function ofcChange(args) {   //
        if (args.e == null)
            return;
        //var listObj = document.getElementById('ddlOfficers').ej2_instances[0];
        $.ajax({
            url: 'Officers/?handler=OfficerInfo',
            type: 'GET',
            dataType: 'json',
            data: {
                OfcID: args.value
            },
            headers: {
                RequestVerificationToken:
                    $('input:hidden[name="__RequestVerificationToken"]').val()
            }
        })
            .done(function (result) {
                        document.getElementById('ddlOfcStatus').ej2_instances[0].value = result.StatusId;  //This does not set the selected item even though the value is present in the datasource

            })
    }
//Here is the function in the ajax call..

        public JsonResult OnGetOfficerInfo(Guid OfcID)
        {
            Officer = res.Officers.Where(a => a.OfficerId == OfcID).FirstOrDefault();  //this returns a table with a StatusId column of type guid.
            return new JsonResult(Officer);
        }


Prince Oliver [Syncfusion]
Replied On March 21, 2019 09:09 AM UTC

Hi Michael, 

Greetings from Syncfusion support. 

From your shared code, we could infer that you require to assign the value and reflect the changes in the control immediately. In that case, we suggest you call the dataBind method once the value is assigned. Please refer the following code. 

function ofcChange(args) {   // 
    if (args.e == null) 
        return; 
    //var listObj = document.getElementById('ddlOfficers').ej2_instances[0]; 
    $.ajax({ 
        url: 'Officers/?handler=OfficerInfo', 
        type: 'GET', 
        dataType: 'json', 
        data: { 
            OfcID: args.value 
        }, 
        headers: { 
            RequestVerificationToken: 
                $('input:hidden[name="__RequestVerificationToken"]').val() 
        } 
    }) 
    .done(function (result) { 
        var obj = document.getElementById('ddlOfcStatus').ej2_instances[0]; 
        obj.value = result.StatusId; 
        obj.dataBind(); // call the dataBind method 
 
    }) 
} 

Please let us know if you need any further assistance on this. 

Regards, 
Prince 


Michael Lambert
Replied On March 22, 2019 06:39 PM UTC

Actually I forgot to add the field declaration.  Work perfectly after that:

            <ejs-dropdownlist id="ddlOfcStatus" dataSource="@Model.Statuses" placeholder="Select Status">
                <e-dropdownlist-fields text="text" value="value"></e-dropdownlist-fields>
            </ejs-dropdownlist>
 Thanks,
Mike

Prince Oliver [Syncfusion]
Replied On March 25, 2019 07:07 AM UTC

Hello Mike, 

Thank you for the update. We are glad that the issue is resolved in your end. Let us know if you need any further assistance on this. 

Regards, 
Prince 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;