使用hiddenfield-value作为asp-route-parameter [英] Use hiddenfield-value for asp-route-parameter
问题描述
我在_PartialView中有一个具有我想要的值的隐藏字段(jQuery将其填满)我需要在(foreach)asp-route-parameter中使用相同的值
Ive got a hidden field in a _PartialView with the value I want (jQuery fills it up) I need this same value in a (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应该从hiddenfield中获取值,并将其作为参数传递
So, asp-route-cropId should get the value from the hiddenfield, to pass it along as parameter
我尝试使用jQuery函数(没有任何运气),例如
I tried using a jQueryfunction (without any luck) like
var neededValue = the value I got from another jQfunction
$('#productCard').attr("asp-route-cropId", neededValue);
可以做到吗?
我将如何处理?谢谢!
Can it be done?
How would i approach this?
Thanks!
推荐答案
通过在a-tag中添加data-producttypeId来解决此问题;将productCard从"id"更改为"class"
Solved this by adding a data-producttypeId in the a-tag; changed productCard from 'id' to '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函数:
I added a jQuery function:
$('.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中后,一切都很好!感谢您的投入,给了我一些很好的线索!
A replace on a string creates a new string instead of actually replacing it. After placing the newly created string in the href, all was good! Thanks for the input guys, gave me some good leads!
这篇关于使用hiddenfield-value作为asp-route-parameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!