This is a migrated thread and some comments may be shown as answers.

Values in dropdown

2 Answers 607 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Ursus
Top achievements
Rank 1
Iron
Ursus asked on 02 Sep 2019, 02:32 PM

My model is quite complex but basically has a list of fields, each field then has a number of values that can be selected via a drop down. I loop through the fields and try and create a dropdown for each field: 

 

    @for (var i = 0; i < Model.Fields.Count; i++) {

            var fieldId = "field_" + i.ToString() + "_value_" + j.ToString();

            @(Html.Kendo().DropDownList()

                   .Name(fieldId)   
                   .Value(Model.Fields[i].Values[j])
                   .Filter("contains")
                   .DataSource(source =>
                          {
                               source.Read(read =>
                               {
                                    read.Action("TypeAhead", "Document", new { docType = Model.DocType, field = i });
                                 })
                                .ServerFiltering(true);
                            })
                     )

 

The DataSource just asks the DB what the predefined (drop down) values are for this field:

        public ActionResult TypeAhead([DataSourceRequest] DataSourceRequest request, string docType, int field)
        {
            var predefinedValues = GetPredefinedValuesForField(docType, field, "");
            return Json(predefinedValues);
        }

 

This works except that the values for the dropdown are being ADDED to each field -> i.e. field 1 has value1 and value2, field2 has value1, value2 (both from field 1) and then it's own values. 

 

It seems as if I have not bound what comes back from the DataSource in the controller to the field but rather to each field in return?

 

Any ideas as to what I am doing incorrectly?

 

 


2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 04 Sep 2019, 02:34 PM
Hello Ursus,

Thank you for the provided code. The issue you are experiencing is probably related to the way the values are retrieved in the TypeAhead Action method.

Attached you will find a project based on the provided code. In the TypeAhead method, I am populating the DropDownLists' DataSource based on the value of the field argument:
public ActionResult TypeAhead(int field)
{
    var values = new List<Field>();
    if(field == 0)
    {
        values.Add(new Field{ Text = "One", Value = "1"  });
        values.Add(new Field{ Text = "Three", Value = "3" });
        values.Add(new Field{ Text = "Five", Value = "5" });
        values.Add(new Field{ Text = "Seven", Value = "7" });
        values.Add(new Field{ Text = "Nine", Value = "9" });
    }
    else if (field == 1)
    {
        values.Add(new Field { Text = "Two", Value = "2" });
        values.Add(new Field { Text = "Four", Value = "4" });
        values.Add(new Field { Text = "Six", Value = "6" });
        values.Add(new Field { Text = "Eight", Value = "8" });
    }
    return Json(values, JsonRequestBehavior.AllowGet);
}

Could you please modify the project with the missing details so that the behavior you are experiencing would be reproduced? I will then provide you with the most appropriate solution.

I am looking forward to your reply.
 
Regards,
Martin
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Ursus
Top achievements
Rank 1
Iron
answered on 05 Sep 2019, 08:45 AM

Hi Martin

thank you for answering -> you are correct, the error is in the data coming from the DB. 

My problem has been solved -> thank you again :)

Cheers

Ursus

Tags
DropDownList
Asked by
Ursus
Top achievements
Rank 1
Iron
Answers by
Martin
Telerik team
Ursus
Top achievements
Rank 1
Iron
Share this question
or