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

Set initial model value in dropdown list

Hi, I have created a dropdown list inside the editor template folder for an asp net core 2 project.
The dataSource comes from a viewbag collection.

This is the dropdown definition:

   <ejs-dropdownlist id="@ViewData.ModelExplorer.Metadata.PropertyName" allowFiltering="true" dataSource="@ViewData[collectionName]" value="@Model">
        <e-dropdownlist-fields text="Text" value="Value" />
    </ejs-dropdownlist>

This is the collection:

ValueText
1Red
10Blue
21Green
43Purple

In Create mode everything is working. The list is filled and the correct value is returned.
When I go in Edit mode, the selected value is passed to the template, because @Model contains it, but the dropdown doesn't show the corresponding text.
If I confirm the view, the correct value is posted back to the controller. This means that the dropdown knows the current value, but it doesn't diplay the associated data.

The dropdown contains the elements, because the page code contains this at the bottom and I can see them if I open the dropdown:

var DropDownListqzmnpn3x1hp=new ejs.dropdowns.DropDownList({
  "allowFiltering": true,
  "dataSource": ejs.data.DataUtil.parse.isJson([....

For testing, if I set the index property it selects an item, but it's not the correct one, because index starts from 0 to collection.count - 1, while the value passed to the template is the value of the selected item (1, 10, 21 or 43).

So, how can I set and display the default value when the view is loaded?

Thanks, Marco


3 Replies

KR Karthik Ravichandran Syncfusion Team June 5, 2019 11:52 AM UTC

Hi Macro, 

Thanks for contacting Syncfusion support. 

We have checked your query. We would like to inform you that reported issue may occurs due to mapping the value fields as a collection(Value= @Model). We have suggested you to set the value as any one particular data only like below. 

<ejs-dropdownlist id="@ViewData.ModelExplorer.Metadata.PropertyName" allowFiltering="true" dataSource="@ViewData[collectionName]" value="@Model.Value"> 
    <e-dropdownlist-fields text="Text" value="Value" /> 
</ejs-dropdownlist> 


Note: Based on your application model data, please change the field name in value API 
 
If we misunderstood your requirement, please share your requirement with more details this will help us to provide the solution at earliest. 

Regards, 
Karthik R 





MA Marco June 6, 2019 02:59 PM UTC

Ciao Karthik,

your solution didn't solve my issue because the model is dynamic and there isn't a value property...The model that I receive in the editor template view is the real property, but after some digging, I've understood the problem.
It's due to type mismatch.
The source for the dropdownlist is a List<SelectListItem> where the key value is a STRING, while in my model the value is a INT.
I had to add value="@(Model.ToString()) and now it works perfectly.

BR
Marco


PO Prince Oliver Syncfusion Team June 7, 2019 06:08 AM UTC

Hello Marco, 

Thanks for your update. We are glad that the issue is resolved in your end. Please let us know if you need any further assistance. 

Regards, 
Prince 


Loader.
Up arrow icon