how to use jquery with razor syntax in asp.net mvc 5?

后端 未结 3 1056
失恋的感觉
失恋的感觉 2021-01-06 04:02

I need to use jQuery to add some elements dynamically. So I looked up in the internet and I found this. It is nice and working when there is plain html elements inside singl

3条回答
  •  庸人自扰
    2021-01-06 04:17

    You cannot add Razor elements using JQuery because, as you have stated, JQuery is a client side library and ASP.NET using Razor syntax is a server side scripting language.

    If you want to add elements created using Razor syntax then add a hidden element to the page and use JQuery to add a clone of it to the DOM.

    Something like this should give you an idea of what I mean:

    @Html.DropDownList("transaction_item", null, htmlAttributes: new { @class = "form-control", @id = 'template-ddl' })
    
    $("#trItem").append($('#template-ddl').clone());
    

提交回复
热议问题