【问题标题】:MVC5 actionLink send textbox value to controllerMVC5 actionLink 将文本框值发送到控制器
【发布时间】:2016-10-20 16:21:38
【问题描述】:

我有文本框

@Html.TextBox("report_id", null, new { @class = "form-control col-md-7 col-xs-12", @Value = @ViewBag.Reports_ID, @readonly = true })

我想使用 actionlink 将文本框值发送到控制器。但我无法获取文本框值。

@Html.ActionLink("Preview", "Update", "Home", new { @report_id = Request.QueryString["report_id "] }, new { @class = "btn btn-info", target = "_blank" })

请帮忙,谢谢

【问题讨论】:

  • 您需要javascript根据文本框中编辑的值更新链接的href属性(或者只是将文本框括在FormMethod.Get的表格中。添加@没有意义987654326@ - 这只是添加 report_id 的原始值

标签: asp.net-mvc


【解决方案1】:

首先给动作链接一个ID属性,如下所示:

@Html.ActionLink("Preview", "Update", "Home", new { @report_id = Request.QueryString["report_id "] }, new { @class = "btn btn-info", target = "_blank", id="alink" })

然后为ActionLink写一个jQuery点击事件函数:

$("#alink").click(function () {
    $(this).attr("href", $(this).attr("href") + "?id=" + $("#report_id").val());
});

看看它是否适合你。

【讨论】:

  • 嗨,费萨尔,谢谢
  • 嗨@Faisal,跟着你的代码,我可以得到文本框ID。但是我按“预览”按钮2次以上,地址链接一般会这么多“report_id”。请参阅“/Reports/Home/Update?report_id=161024001?report_id=161024001?report_id=161024001”。
  • 这种情况下需要检查id参数是否已经存在再添加。类似于: var url =$(this).attr("href"); if(url.indexOf('?report_id=') == -1) {$(this).attr("href", $(this).attr("href") + "?report_id=" + $(" #report_id").val());}
  • 太棒了!很高兴知道它对您有所帮助。请标记答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多