【问题标题】:Javascript Link to AspCore ActionJavascript 链接到 Asp 核心操作
【发布时间】:2021-03-20 06:42:26
【问题描述】:

我想在 aspcore 中传递一个 Javascript 创建的字符串作为路由数据。该字符串是从 Javascript 数组中创建的,如下所示:

<body>
    FULL LIST: <span id="Inventory"></span>
</body>    
<script>
var FullString = LargeList.concat(SmallList);
    LenB = BoxList.length;
    FullString.push(LenB);

    document.getElementById("Inventory").innerHTML = FullString.join("/");
</script>

我想我可以再次使用 Javascript 来创建链接,但这似乎不是正确的想法。

<body>
    <input type="button" onclick="AddToFiv()" value="Create A 5/I Remark" />
    <p id="FivRemarkLink"></p>
</body>
<script>
    function AddToFiv() {

    var FullString = LargeList.concat(SmallList);
    LenB = BoxList.length;
    FullString.push(LenB);

    var Inventory = FullString.join("/");

    var text = "<a asp-action="FivCreate" asp-route-zero="@ViewBag.zero" asp-route-code="I" asp-route-priority="5" asp-route-remark=" + Inventory + ">ADD 5/I</a>";

    document.getElementById("FivRemarkLink").value = text;
}
</script>

【问题讨论】:

    标签: javascript c# asp.net-core model-view-controller


    【解决方案1】:

    首先js中无法识别asp-xxx,所以可以使用href,然后如果要在&lt;p id="FivRemarkLink"&gt;&lt;/p&gt;中添加&lt;a&gt;&lt;/a&gt;,可以这样使用document.getElementById("FivRemarkLink").innerHTML = text;

    这是一个带有假数据的演示(ViewBag.zero="zero"):

    <input type="button" onclick="AddToFiv()" value="Create A 5/I Remark" />
    <p id="FivRemarkLink"></p>
    <script>
    
        //$(function () {
    
        //    const LargeList = ['a', 'b', 'c'];
        //    const SmallList = ['d', 'e', 'f'];
        //    const BoxList = ['g', 'h', 'i'];
        //    var FullString = LargeList.concat(SmallList);
        //    LenB = BoxList.length;
        //    FullString.push(LenB);
    
        //    document.getElementById("Inventory").innerHTML = FullString.join("/");
        //})
         function AddToFiv() {
             const LargeList = ['a', 'b', 'c'];
             const SmallList = ['d', 'e', 'f'];
             const BoxList = ['g', 'h', 'i'];
        var FullString = LargeList.concat(SmallList);
        LenB = BoxList.length;
        FullString.push(LenB);
    
        var Inventory = FullString.join("/");
             var text = "<a href='FivCreate?zero=@ViewBag.zero&code=I&priority=5&remark="+Inventory+"'>ADD 5/I</a>";
             document.getElementById("FivRemarkLink").innerHTML = text;
         }
        </script>
    

    结果:

    【讨论】:

    • 好的,但这似乎并不需要采取行动。除非我错过了什么。我需要链接来调用操作并将字符串传递到操作和新页面。
    • 是的,我点击链接“Add5/I”,它会调用动作,你能分享更多关于你的代码,比如你的动作。这样我就可以试着找出你不能去的原因到行动。
    猜你喜欢
    • 1970-01-01
    • 2018-03-23
    • 2017-03-22
    • 2018-07-15
    • 2019-03-20
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    相关资源
    最近更新 更多