Javascript call with parameter from inside a template

6 posts, 0 answers
  1. Laurie
    Laurie avatar
    141 posts
    Member since:
    Feb 2007

    Posted 10 Apr 2015 Link to this post

    I have the following code that I am trying to use to delete an item in the database when someone clicks the delete button:
     

    <script id="treeview-template" type="text/kendo-ui-template">
             #: item.name #
             <a href="javascript:void(0)" class='btn-sm btn-danger' onclick="DeleteNode(@item.id)">x</a>
     </script>
     @Html.Kendo().TreeView().Name("Categories").ExpandAll(true).Template("treeview-template").DataSource(dataSource => dataSource
    .Model(model => model
    .Id("id")
             .HasChildren("hasChildren")
             )
               .Read(read => read.Action("GetCategories", "Categories"))).DataTextField("name").DragAndDrop(true).Checkboxes(true).Events(events => events.Change("onChange").Select("onSelect").Check("onCheck").Collapse("onCollapse").Expand("onExpand").DragStart("onDragStart").Drag("onDrag").Drop("onDrop").DragEnd("onDragEnd"))

    The issue I'm having is that the field I'm trying to pass to the function, @item.id, is causing the exception: "The name 'item' does not exist in the current context." How can I format the code so this works?

    Thanks.

    Laurie

  2. Laurie
    Laurie avatar
    141 posts
    Member since:
    Feb 2007

    Posted 10 Apr 2015 in reply to Laurie Link to this post

    Please note: I do have .TemplateId in my code rather than .Template as is in the example I gave, and it does work when I don't try to add the javascript call.
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Laurie
    Laurie avatar
    141 posts
    Member since:
    Feb 2007

    Posted 13 Apr 2015 Link to this post

    Since I haven't received a response, I'm going to post a ticket for this issue. I'll let y'all know what the upshot is.
  5. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2153 posts

    Posted 14 Apr 2015 Link to this post

    Hello Laurie,

    I covered the question in the support thread on the same topic. Let me know if the information was helpful.

    Regards,
    Dimiter Madjarov
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  6. Laurie
    Laurie avatar
    141 posts
    Member since:
    Feb 2007

    Posted 14 Apr 2015 in reply to Dimiter Madjarov Link to this post

    Yes it was.  For those who are interested, the correct syntax is:

    <a href='javascript:void(0)' class='btn-sm btn-danger' onclick='DeleteNode(#:item.id#)'>x</a>

    Visual Studio 2013 will mark the hash marks in red, and when you hover over them will tell you it is an "Invalid character," but don't let that scare you.  It works just fine.

  7. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2153 posts

    Posted 15 Apr 2015 Link to this post

    Hello Laurie,

    Thank you for sharing the approach with the community. I am glad the issue is resolved.

    Regards,
    Dimiter Madjarov
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top
UI for ASP.NET MVC is VS 2017 Ready