我可以在js文件中添加asp帮助器吗?

时间:2019-12-07 14:50:43

标签: javascript .net core

我将_Layout.cshtml中的JavaScript代码动态生成为@ Html.ActionLink,将所有JavaScript代码移动到js文件中以组织项目后,@ Html.ActionLink将不再编译,它将显示为字符串

async function getGamesJson() {
        const response = await fetch('/Games/GetGamesJson');
        const gamesJson = await response.json();
        var isFirstItem = true;
        $ul = $('#GamesAfter');
        //$ul2 = $('#test2');
        //console.log(gamesJson[0])
        for (var i = 0; i < gamesJson.length; i++) {
            var a = '@Html.ActionLink("Game Name", "Run", "Games", new { releaseName = "releaseGameNamePlaceHolder" }, new { @class = "nav-link text-dark" })';
            a = a.replace('Game Name', gamesJson[i].name);
            a = a.replace('releaseGameNamePlaceHolder', gamesJson[i].releaseName);
            $ul.append(`
                    <li class="nav-item" id="GameMenue-${i}">
                    `+a+`
                    </li>
            `);
            if (isFirstItem) {
                $(`#GameMenue-${i}`).insertAfter('#GamesAfter');
                isFirstItem = false;
            } else {
                 $(`#GameMenue-${i}`).insertAfter(`#GameMenue-${i-1}`);
            }

        }
    }

我能做些什么吗?还是应该将代码放回到_Layout.cshtml中?

谢谢

0 个答案:

没有答案