我有一个LocationCode下拉列表:
@Html.DropDownList("LocationCode", (SelectList)ViewBag.Values, "Search Location...", new { @value = "@ViewBag.LocationCode.ToString()", @class = "form-control btn btn-default dropdown-toggle", name = "LocationCode" })
以下是我的@Html.ActionLink
,它调用导出到Excel功能:
@Html.ActionLink("Export to Excel", "ExportAllVehicleLocationsToExcel", new { id = "ExportAllVehicleLocationsToExcel" })
这是我的Javascript尝试无效:
jQuery(function () {
$("#ExportAllVehicleLocationsToExcel").click(
function AllVehicleLocationsSearch() {
$('#LocationCode').val(LocationCode);
}
);
});
如何使用Javascript将选定的LocationCode传递给@Html.ActionLink
?
答案 0 :(得分:1)
您需要根据所选值
构建新网址生成应该生成的代码应该只是(在使用value
方法时从不设置HtmlHelper
属性,幸运的是,设置name
属性的尝试根本不做任何事情)< / p>
@Html.DropDownList("LocationCode", (SelectList)ViewBag.Values, "Search Location...",
new { @class = "form-control btn btn-default dropdown-toggle" })
,链接可以只是
<a href="#" id="export">Export to Excel</a>
然后脚本将是
var baseUrl = '@Url.Action("ExportAllVehicleLocationsToExcel")';
$('#export').click(function() {
location.href = baseUrl + '/' + $('#LocationCode').val();
})