Dynamically create html action links using javascr

2019-08-18 01:02发布

问题:

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>';