DropDownListFor odd template behaviour - cannot retrieve correct data

2 posts, 0 answers
  1. John
    John avatar
    23 posts
    Member since:
    Nov 2012

    Posted 14 Jun 2013 Link to this post


    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");
                        .Template("<img src='" + @Url.Action("GetImage", "Home", new { imageId = "#=data#"}) + "' width='128' height ='51'/>")

    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?
  2. John
    John avatar
    23 posts
    Member since:
    Nov 2012

    Posted 17 Jun 2013 Link to this post

    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'........ 
  3. UI for ASP.NET AJAX banner
Back to Top