c# - 将 hiddenfield-value 用于 asp-route-parameter

标签 c# jquery asp.net-core-mvc

我在 _PartialView 中有一个隐藏字段,其中包含我想要的值(jQuery 填充它) 我在 (foreach) asp-route-parameter 中需要相同的值

<div class="js-products-list">
 <input id="cropIdForProdBaseJQ" type="hidden" value="" />
  @foreach (var product in Model.Products)
  {
    <a asp-controller="XXX" asp-action="YYY" asp-route-language="@Model.CurrentLanguage" asp-route-cropId="" id="productCard">
  }

因此,asp-route-cropId 应该从隐藏字段中获取值,并将其作为参数传递

我尝试使用像

这样的 jQuery 函数(没有任何运气)
var neededValue = the value I got from another jQfunction
$('#productCard').attr("asp-route-cropId", neededValue);

可以吗?
我将如何处理这个? 谢谢!

最佳答案

通过在 a-tag 中添加 data-producttypeId 解决了这个问题;将 productCard 从“id”更改为“class”

@foreach (var product in Model.Products)

{
<a asp-controller="XXX" asp-action="YYY" asp-route-language="@Model.CurrentLanguage" asp-route-cropId="" class="productCard" 
data-producttypeId="@product.ProductTypeId">
}

我添加了一个 jQuery 函数:

$('.productCard').click(function(e) {    
var cropId = $('#croptype-filter-From-ProductType').val();

var clickedCard = $(this);
var baseUrl = clickedCard.attr("href");

var newUrl = baseUrl.replace("crop-ID", cropId);
window.location.href = newUrl;
});

字符串上的替换会创建一个新字符串,而不是实际替换它。 将新创建的字符串放入 href 后,一切都很好! 感谢提供意见的人,给了我一些很好的线索!

关于c# - 将 hiddenfield-value 用于 asp-route-parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57144468/

相关文章:

c# - WinForm 弹出窗体与父级的关系

c# - 使用智能在 c# 中取缔字符串

c# - 如何手动设置Windows窗体位置

javascript - 如何仅在页面加载时在 <body> 的特定 div 中显示加载程序

c# - 如何删除默认的 ASP.NET Core Identity 端点?

c# - 确定对象是否是特定类型或派生自特定类型?

javascript - 将 PHP 添加到 HTML 文件后 JQuery 不工作

jquery - 无法在 jqgrid 中选择行

asp.net-mvc - 自定义 .NET Core 标记帮助程序不起作用

javascript - Asp.Net Core 2.1 请求排除客户端 cookie