Dynamically create html action links using javascript variables on ASP.NET MVC3 razor

戏子无情 提交于 2019-12-12 04:36:08

问题


Say I have a javascript dictionary variable with the following key value pairs:

var attributes = { name : "name1", age: 10}

I want to use the values in the above attribute to create the children of an HTML ul element within the same javascript function as follows:

var htmlContent = '<ul><li>@Html.ActionLink(attributes["name"], "Details", "Home", new { name = attributes["name"]})</li></ul>'

Is there a way to do this? TIA.


回答1:


I'm not sure you can use the JavaScript attributes variable in the @Html.ActionLink?

I'd recommend changing it to

var htmlContent = '<ul><li><a href="@Url.Action("Details", "Home")" name="' +
    attributes.name + '">Link</a></li></ul>';


来源:https://stackoverflow.com/questions/9525702/dynamically-create-html-action-links-using-javascript-variables-on-asp-net-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!