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

DropDownListFor complex objects

1 Answer 72 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 11 Jan 2017, 09:46 PM

With some help from the forums and some trial and error I have created an editor that works just fine for complex objects when they are in a kendo grid.

i.e. when I have a complex object as a column in a grid this editor will edit the object and populate the complex object on the post back to the controller

 

However, when I am trying to use this editor in a web form ("Ajax.BeignForm") and do my post back I get an error along the lines of "The parameter conversion from type 'System.String' to type 'X' failed because no type converter can convert between these types".

 

  1. Why does this work just fine for grids, but not forms? (I'm guessing grids do some "magic" that handles the casting)
  2. Are there any options for having a DropDownList or similar control update a complex object instead of just a hidden string field?

 

 

 

@using System.Text.RegularExpressions;
 
@model object
 
@{       
    Dictionary<string, object> metaData = ViewData.ModelMetadata.AdditionalValues;
    ListType? ListType_DataAnnotation = (ListType?)(metaData.ContainsKey("ListType") ? metaData["ListType"] : null);
    string Param1_DataAnnotation = (string)(metaData.ContainsKey("Param1") ? metaData["Param1"].ToString() : null);
    string Param2_DataAnnotation = (string)(metaData.ContainsKey("Param2") ? metaData["Param2"].ToString() : null);
    string Param3_DataAnnotation = (string)(metaData.ContainsKey("Param3") ? metaData["Param3"].ToString() : null);
    bool? ShowLabel_DataAnnotation = (bool)(metaData.ContainsKey("ShowLabel") ? metaData["ShowLabel"] : false);
    string ParamFunction_DataAnnotation = (string)(metaData.ContainsKey("ParamFunction") ? metaData["ParamFunction"].ToString() : null);
 
    ListType? ListType_VD = (ListType?)ViewData["ListType"];
    string Param1_VD = (string)ViewData["Param1"];// ?? Param1_DataAnnotation;
    string Param2_VD = (string)ViewData["Param2"];// ?? Param2_DataAnnotation;
    string Param3_VD = (string)ViewData["Param3"];// ?? Param3_DataAnnotation;
    bool? ShowLabel_VD = (bool?)ViewData["ShowLabel"];
    string ParamFunction_VD = (string)ViewData["ParamFunction"];// ?? ParamFunction_DataAnnotation;
         
    ListType ListType = (ListType)(ListType_VD ?? ListType_DataAnnotation); //one of these will always have a value in them
    string Param1 = Param1_VD ?? Param1_DataAnnotation;
    string Param2 = Param2_VD ?? Param2_DataAnnotation;
    string Param3 = Param3_VD ?? Param3_DataAnnotation;
    bool ShowLabel = (ShowLabel_VD ?? false) || (ShowLabel_DataAnnotation ?? false);
    string ParamFunction = ParamFunction_VD ?? ParamFunction_DataAnnotation;
 
    string uniqueName = Regex.Replace(Guid.NewGuid().ToString(), "[^A-Za-z]+", "");
 
    string ID = (string)ViewData["ID"];
    ID = ID ?? uniqueName;
    string DataTextField = ListService.GetDataTextField(ListType);
    string DataValueField = ListService.GetDataValueField(ListType);       
}
 
@if(ShowLabel)
{
    @Html.LabelFor(x => x)
}
@(Html.Kendo().DropDownListFor(x => x)
    .DataTextField(DataTextField)
    .DataValueField(DataValueField)
    .Filter(FilterType.Contains)
    .DataSource(source =>
    {
        source.Read(read =>
        {
            if (ParamFunction == null || ParamFunction.Length == 0)
            {
                read.Action("Read", "List", new { ListType = ListType, Param1 = Param1, Param2 = Param2, Param3 = Param3 });
            }
            else
            {
                read.Action("Read", "List").Data(ParamFunction);
            }
        });
    })   
)

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 13 Jan 2017, 01:44 PM
Hello Alex,

You can see Georgi's post in this forum thread, which provides more info on why this exception is thrown and on the expected DefaultModelBinder format.

Regards,
Ivan Danchev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Alex
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or