【问题标题】:Is it possible to add dynamic value to @Html.ActionLink param是否可以将动态值添加到 @Html.ActionLink 参数
【发布时间】:2017-06-22 20:23:03
【问题描述】:

我的 MVC 控制器中有一个方法,当被调用时会下载一个文档。

我有一个引导模式,href 所在的位置指向此方法。

@Html.ActionLink("Download", "Download", new { applicantId = 1, templateId = 1 })

分钟的申请者ID 和模板ID 是硬编码的,但是这些值通过jquery 作为文本值传递给Modal。

是否可以动态获取 HTML id 并将其添加到 @Html.ActionLink?

模态:

<div id="template1">
    <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <h3 id="app" style="display:none"></h3>
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myModalLabel">Letter Template Logo</h4>
                </div>
                <div class="modal-body">
                    <img style="background-size:cover;" src="~/Content/images/image.png" alt="" />
                </div>
                <div class="modal-footer">
                 ActionLink("Download", "Download", new { applicantId = 1, templateId = 1 })
                </div>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 不太清楚你在这里问什么。 @Html.ActionLink() 是剃须刀代码,在发送到视图之前在服务器上进行解析,因此如果您想根据仅在客户端可用的数据更改生成的 href 属性,那么您需要 javascript。而且您不能使用 ajax 下载文件。您需要重定向到该方法。
  • @StephenMuecke 是的,发布后,我读到无法通过 ajax 做到这一点。我在问,我可以将传递给模态的 a 值绑定到 html.actionlink 参数吗?
  • 你需要使用javascript/jquery来改变href属性。或者,取消默认操作并构建您自己的 url 并使用 location.href 重定向和下载文件。 (当您没有显示控制器方法或指出applicantIdtemplateId 的值来自哪里时,有点难以给出答案)
  • 这些值来自下拉列表,所以如果我可以使用 javascript 更改属性,使用“a”标签而不是 @html.actionlink 会更好吗?
  • 没什么区别(@Html.ActionLink() 只是生成一个&lt;a&gt; 标签 - 使用ActionLink() 总是更好,因为它可以确保您的网址正确)

标签: javascript c# jquery asp.net-mvc asp.net-mvc-5


【解决方案1】:

我创建了 .NetFiddle。它可以工作here

您可以使用.replace() 方法更改Html.ActionLink() 方法参数,您可以如下实现。

//html

Parameter 1:<input id="param1" />
Parameter 2:<input id="param2" />

<div class="modal-footer">
    @Html.ActionLink("Download", "Example", new { applicantId = 1, templateId = 1 })
</div>

<button id="yourbutton">Change Action Link</button>

//jquery

<script type="text/javascript">         
$("#yourbutton").on("click",function(){

    var param1 = $("#param1").val();
    var param2 = $("#param2").val();
    console.log(param1);
    console.log(param2);

    var link = '@Html.ActionLink("Download", "Example", 
                    new { applicantId  = "_applicantId_",  templateId = "_templateId_"})';
    if(param1 == "" || param2 == "")
    {
        alert("please fill all parameter inputs")
    }
    else
    {
        link = link.replace('_applicantId_', param1);
        link = link.replace('_templateId_', param2);
        $(".modal-footer").html(link)
        alert("I have changed ActionLink. Please click the download link")

    }
})
</script>

【讨论】:

  • 谢谢,最后我确实做到了。我很喜欢你的解决方案。所以会投票:)
  • 欢迎您 :) 如果此解决方案适用于您的问题,请考虑接受答案以帮助人们理解它已解决 :)
  • 实际尝试过,它只是将链接作为文本附加到模式页脚中
  • @Haris 您的模态页脚右侧只有操作链接
  • 是的,它只有一个,是的,小提琴作品一定是我在做的事对不起
【解决方案2】:
@Html.ActionLink("Pond", "Pond", "Home", htmlAttributes: new  { 
@class="classNameYouWantToAdd" })

@Html.ActionLink("Pond", "Pond", "Home", new  { 
@class="classNameYouWantToAdd" })

如果您想从 JQuery 中执行此操作,那么您可以定位包含链接的 li 标记,或者像上面一样将类名添加到您的操作链接,然后像往常一样更新对象的类。

 $('.TargetedClassNameOfActionLink').addClass('addDifferentClass').removeClass('removeSomeOtherClass')





$('#liTagId').find('.ClassNameOfActionLink').addClass('addDifferentClass').removeClass('removeSomeOtherClass')

$('#tabPond').addClass("greenTab");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-18
    • 2012-08-05
    • 2021-06-06
    • 2012-07-22
    • 1970-01-01
    • 2019-01-17
    • 2015-06-19
    • 1970-01-01
    相关资源
    最近更新 更多