【问题标题】:Using select, option value inside the actionlink在 actionlink 中使用 select、option 值
【发布时间】:2020-01-18 10:12:40
【问题描述】:
if (@ViewBag.Title == "Title")
{

@ViewBag.Title
<div class="btnNEW" hidden id="fqXLS">
<select id = "fqcXLS">
<option id ="fqcXLSa">A</option>
<option id ="fqcXLSb">B</option>
<option id ="fqcXLSc">C</option>
<option id ="fqcXLSd">D</option>
</select>

@Html.ActionLink("Export To Excel", "ExportToExcel", "EEC", new { option = ??? }, new { @class = "cancel" })
</div>
}

我能够通过执行 new{option="A"} 使其工作,但这只会发送“A”

如何使用选定的值?

【问题讨论】:

  • 看起来您的&lt;option&gt; 标签都没有填充value 属性see the MDN documentation。这也取决于你想如何使用选定的值?如果在 Javascript 中,请查看此答案 stackoverflow.com/a/1085810/3338349
  • 那么,在用值设置我的选项之后,如何从 actionlink 传递它?选项 = document.getElementById("fqxXLS").value ?或类似的东西?
  • 我也试过@Html.ActionLink("Export To Excel", "ExportToExcel", "EEC", new { option= "getCategory();" }, new { @class= " cancel" }) 创建一个函数来返回 ID 的值,然后在 actionlink 中调用该函数。好像不行

标签: c# parameters html.actionlink


【解决方案1】:

第一次呈现页面时会创建 ActionLink。当用户从下拉列表中选择时它不会更新(它将具有相同的初始值)。因此,您需要使用 javascript 或 jquery 更新锚点 href 值。

有很多方法可以实现,下面是一个不显眼的方法:

1) 给 ActionLink 一个 ID:

@Html.ActionLink("Export To Excel", 
                 "ExportToExcel", 
                 "EEC", 
                  null, 
                  new { @id = "lnkCancel", @class = "cancel" })

2) 为下拉列表的 onchange 事件添加事件监听器(应该在文档准备好/加载后调用):

// get the dropdownlist
var ddl = document.getElementById('fqcXLS');

// add event listener for 'onchange'
ddl.addEventListener('change', function () {
    var url = '@Url.Action("ExportToExcel", "EEC")';           // base url
    var link = document.getElementById('lnkCancel');           // get link to update 
    var selectedValue = ddl.options[ddl.selectedIndex].value;  // get selected value
    link.href = url + '?option=' + selectedValue;              // update link href
}); 

这将在选择列表更改时更新锚点 href 属性。

var ddl = document.getElementById('mySelect');

ddl.addEventListener('change', function () {

  var url = 'somesite/myaction';           // base url
  var link = document.getElementById('lnkCancel');           // get link to update 
  var selectedValue = ddl.options[ddl.selectedIndex].value;  // get selected value
  link.href = url + '?option=' + selectedValue;              // update link href
  
  link.innerText = link.href;  // dont include this, this just shows the url text
});
<select id="mySelect" >
	   <option value="A" selected>A</option>
	   <option value="B">B</option>
	   <option value="C">C</option>
	   <option value="D">D</option>
	   <option value="E">E</option>
	</select>
  
  <a href="somesite/myaction" id="lnkCancel" >Select item in dropdown</a>

【讨论】:

  • #1 给 actionlink 一个 id ``` => @Html.ActionLink("Export To Excel", "ExportToExcel", "EEC", new { @id = "fqcXLS" }, new { @class= "取消" }) ``` 像这样?
  • 它不是像我写的那样工作吗? @locomotive @id 应该在设置了@class 标签的HtmlAttributes 下设置。
  • 您也可以将Html.ActionLink 替换为ID为&lt;a id="myId"&gt;的常规锚标记。
  • 你写的方式?我不知道 lnkCancel 是干什么用的
  • 您需要在更改选择列表时更改链接的URL。一种方法是为链接 (ActionLink) 提供一个 ID,以便您可以更改该值。 lnkCancel 只是我给它起的名字,你可以给它起任何名字。你需要使用 javascsript 或 jquery 来做你想做的事,没有它是不可能的。 @机车
猜你喜欢
  • 2017-12-14
  • 2019-02-07
  • 1970-01-01
  • 2019-02-09
  • 2020-11-20
  • 2019-01-28
  • 2016-12-08
  • 2020-12-28
  • 2014-05-05
相关资源
最近更新 更多