Select dropdown by text or value, not index

How do I set the selected text/value of a dropdown by text or value.

ie.         @{Html.EJ().DropDownList("#ddlBU").setSelectedText("Hello");}


3 Replies

PO Prince Oliver Syncfusion Team February 15, 2018 06:36 AM UTC

Hi Michael,   
  
Thank you for contacting Syncfusion Support.   
  
To set the selected Text in the DropDownList, you can use the Text property in the DropDownList. Kindly refer to the following code.   

@Html.EJ().DropDownList("ddlBU1").Datasource((IEnumerable<Languages>)ViewBag.datasource).DropDownListFields(f => f.Text("text").Value("value")).Text("JavaScript") 


To set the selected Value in the DropDownList, you can use the Value property in the DropDownList. Kindly refer to the following code.   

@Html.EJ().DropDownList("ddlBU2").Datasource((IEnumerable<Languages>)ViewBag.datasource).DropDownListFields(f => f.Text("text").Value("value")).Value("3") 

For more information, kindly refer to the following UG documentation link: https://help.syncfusion.com/aspnetmvc/dropdownlist/getting-started#setting-and-getting-value   
  
We have attached  a sample for your reference, Please find the sample at the following location:http://www.syncfusion.com/downloads/support/forum/135943/ze/DDLselectTextandValue1452981799   
  
Regards,   
Prince   




ML Michael Lambert February 15, 2018 05:14 PM UTC

That doesn't work.  I don't need to set the datasource and everything also.  Just programmatically change the selection like:
 $(function () {
       @Html.EJ().DropDownList("ddlBU").Text("ISBU")
    });




<ej-drop-down-list id="ddlBU" watermark-text="Select BU" change="ddlBUChanged" >
    <e-datamanager id="Data" json="(IEnumerable<object>)ViewBag.BUs"></e-datamanager>
    <e-drop-down-list-fields text="text" id="id" value="text" />
</ej-drop-down-list>


PO Prince Oliver Syncfusion Team February 16, 2018 07:21 AM UTC

Hi Michael,   
  
Thank you for your update.   
  
To set the value and text programmatically once the control is rendered, you need to use the control’s instance to access the Text and Value property and set the required values. Kindly refer to the following code snippet.   

<ej-drop-down-list id="ddlBU" datasource="ViewBag.datasource" watermark-text="Select BU" > 
    <e-drop-down-list-fields text="text" value="id" /> 
</ej-drop-down-list> 
<br /> 
<button id="text" onclick="textChange()" > Change text </button> 
<br /> 
<button id="text" onclick="valueChange()"> Change value </button> 
<script> 
    function textChange() {  
        var ddlObj = $("#ddlBU").data("ejDropDownList"); 
        ddlObj.setModel({ text: "CBR 150-R" }); 
    } 
    function valueChange() { 
        var ddlObj = $("#ddlBU").data("ejDropDownList"); 
        ddlObj.setModel({ value: "bk10" }); 
    } 
</script> 

We have attached a sample for  your reference, please find the sample at the following location:http://www.syncfusion.com/downloads/support/forum/135943/ze/DDLselectValueAndText-370537599   
  
For more information, kindly refer to the following UG documentation: https://help.syncfusion.com/js/api-configuration#after-initialization   
  
Regards,   
Prince   



Loader.
Up arrow icon