Embed Html.ActionLink in Javascript in Razor

前端 未结 1 1977
醉话见心
醉话见心 2021-01-15 17:00

I know that it\'s possible to embed @Html in Javascript in MVC3, but I can\'t get the following to work and not sure if this is possible yet.

Using jQuery DataTable,

相关标签:
1条回答
  • 2021-01-15 17:31

    I think it's a simple as adding quotes around the link:

    var result = '@Html.ActionLink("Edit", "EditPallet", new { id = 1 })';
    

    This will generate the whole <a> tag. What you could do as well is just return the url:

    var result  = '@Url.Action("EditPallet", new { id = 1 })';
    

    and the embed it in an existing anchor using jQuery:

    <!-- let's imagine this already exists -->
    <a href="#" id="dynamicLink">Edit</a>
    
    // result is ovbiously what the other function returns
    $("#dynamicLink").attr("href", result);
    
    0 讨论(0)
提交回复
热议问题