问题
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