3 回答

TA貢獻1859條經(jīng)驗 獲得超6個贊
生成 html 時會消耗 razor 屬性。如果您從 javascript 引用此內(nèi)容,則需要一個在頁面編譯后保留的屬性。這對于您的情況來說很棘手,因為您試圖在請求完成后具體化路線。
相反,將輸入值傳遞給操作的良好語義方式是使用表單,而不是嘗試動態(tài)構建錨點。
<form method="get" asp-controller="XXX" asp-action="YYY" asp-route-language="@Model.CurrentLanguage">
<input id="cropIdForProdBaseJQ" type="hidden" value="" />
@foreach (var product in Model.Products)
{
@* Assuming you are doing something useful with "product" here *@
<button name="PassThisId" value="@product.ProductId">Press me!</button>
}
</form>

TA貢獻1807條經(jīng)驗 獲得超9個贊
通過在 a 標簽中添加 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 函數(shù):
$('.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;
});
對字符串的替換會創(chuàng)建一個新字符串,而不是實際替換它。將新創(chuàng)建的字符串放入 href 后,一切都很好!感謝大家的意見,給了我一些很好的線索!

TA貢獻1871條經(jīng)驗 獲得超8個贊
razor 視圖中的標簽助手在服務器上執(zhí)行,其結果(HTML 標記)將發(fā)送到瀏覽器,瀏覽器將呈現(xiàn)它。所以此時anhcor tag helper已經(jīng)生成了鏈接的href屬性值。
您可以做的是,使用 JavaScript 覆蓋正常的鏈接單擊行為,從頁面讀取輸入元素值,并使用它構建新的 URL 并導航到該值。
您可以為您的 CropId 參數(shù)指定一個虛擬值。
<a asp-controller="invoices" asp-action="GetData" asp-route-name="@Model.supplierName" asp-route-cropId=1 id="productCard">Click me</a>
在 JavaScript 中
$("#productCard").click(function (e) {
// Stop the normal navigation
e.preventDefault();
//Build the new URL
var url = $(this).attr("href");
var date = $("#cropIdForProdBaseJQ").val();
url = url.replace(1, date);
//Navigate to the new URL
window.location.href = url;
});
- 3 回答
- 0 關注
- 153 瀏覽
添加回答
舉報