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

DropDownListFor odd template behaviour - cannot retrieve correct data

1 Answer 72 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 14 Jun 2013, 08:57 AM
Hi,

I have a dropdownlistfor that uses templates to display an image for each dropdown entry

I am having trouble retrieving the correct data.

   @(Html.Kendo().DropDownListFor(model => model.Image0Id)
                    .DataSource(source =>
                    {
                        source.Read(read =>
                        {
                            read.Action("GetImageIds", "Home");
                            read.Type(HttpVerbs.Post);
                        });
                    })
                    .Template("<img src='" + @Url.Action("GetImage", "Home", new { imageId = "#=data#"}) + "' width='128' height ='51'/>")
                    .OptionLabel("None")
                    .AutoBind(true)
                )

The #=data# is actually returning my ViewModel so when pass value to GetImage() I have a problem.
If I replace #=data# with say the number 2, the template works and fetches my image with id of 2.
If I insert some javascript #{alert(data)#} to alert me of the data values returned by GetImageIds() it returns the expected values.

I am using this in a similar scenario elsewhere where my template is as follows:
.Template("<img src='" + @Url.Content("~/Images/Banners/#=data#") + "' width='128' height ='51'/>")

This works perfectly.

Anybody have any ideas here?
 

1 Answer, 1 is accepted

Sort by
0
John
Top achievements
Rank 1
answered on 17 Jun 2013, 08:39 AM
Found the answer....

When using the MVC helper the  sharp symbols #= #  are encoded and they are no longer able to be evaluated. 

Resolved this by mixing the template string:

.Template("<img src='" + @Url.Action("GetImage", "Home")+"?imageId=#=data#' width ='128'........ 
Tags
DropDownList
Asked by
John
Top achievements
Rank 1
Answers by
John
Top achievements
Rank 1
Share this question
or