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. Image for the cookie policy date
close icon

How to get dropdownlist current selected value in asp.net core

@{Html.EJ().DropDownList("teamList").Datasource((string[])ViewData["Teams"]).Render();} 

How can I get current DropDownList's selected value in javascript?

5 Replies

KR Keerthana Rajendran Syncfusion Team January 2, 2018 08:55 AM UTC

Hi Xavier,  
  
Thank you for contacting Syncfusion support.  
  
We suggest you to use getSelectedValue method to get the selected value of DropDownList. Please refer to the below given code  
  
function onclick() {  
        var ddlobj = $("#teamList").data("ejDropDownList");  
        alert(ddlobj.getSelectedValue())  
    }  
  
You can also get the selected text and value during change event of DropDownList. Please refer to the below given UG Link  
  
  
  
Regards,  
Keerthana.  



TI Timo January 2, 2018 09:08 AM UTC

Hi Keerthana Rajendran,

Thank you. It worked well.

Best Regards,
Xavier


KR Keerthana Rajendran Syncfusion Team January 3, 2018 04:12 AM UTC

  
Hi Xavier, 
 
Most Welcome. Please get back to us if you require further assistance on this. We will be happy to assist you. 
 
Regards, 
Keerthana. 
 



DW david wright May 2, 2019 10:11 AM UTC

Hi,

I am looking at this thread and I need some more help. 

I am using .NET Core with Razor pages.
I have the following code:

C# IconField:
public class IconField : IField
    {
        [JsonIgnore] public List<Icon> Icons { get; set; }

        public Icon SelectedIcon { get; set; }
    }
}

CSHTML IconField view:
<ejs-dropdownlist id="icons" placeholder="Select an icon" filterBarPlaceholder="Search..." allowFiltering="true"
                      filtering="onFiltering" dataSource="@Model.Icons" popupHeight="300px">
    <e-dropdownlist-fields text="Name" value="Name" iconCss="Name" id="Name"></e-dropdownlist-fields>
</ejs-dropdownlist>
<script type="text/javascript">
    function onFiltering(e) {
        var query = new ej.data.Query();
        query = (e.text !== '') ? query.where('Name', 'contains', e.text, true) : query;
        e.updateData(@Html.Raw(JsonConvert.SerializeObject(Model.Icons)), query);
    }
</script>

The one thing I am missing is saving the selected item to my "SelectedIcon" property on the C# side.

How would I achieve that?


PO Prince Oliver Syncfusion Team May 3, 2019 06:47 AM UTC

Hi Xavier, 

Thank you for the update. 

You can use the model binding to get the selected item in the C# side. Kindly refer to the following code snippet. 

<ejs-dropdownlist id="icons" ejs-for="SelectedIcon.Name" placeholder="Select an icon" filterBarPlaceholder="Search..." allowFiltering="true" 
                filtering="onFiltering" dataSource="@Model.Icons" popupHeight="300px"> 
    <e-dropdownlist-fields text="Name" value="Name" iconCss="Name" id="Name"></e-dropdownlist-fields> 
</ejs-dropdownlist> 
 

[HttpPost] 
public async Task<IActionResult> OnPostAsync(Icon SelectedIcon) 
{ 
    if (!ModelState.IsValid) 
    { 
        return Page(); 
    } 
    // Access the selected value here 
    string selected = SelectedIcon.Name; 
    return Page(); 
} 
 

We have attached the example for your reference, please find it in the following location: http://www.syncfusion.com/downloads/support/forum/135221/ze/DROPDO~1386051148  

Let us know if you have any further queries. 

Regards, 
Prince 


Loader.
Up arrow icon