亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對 asp-route-parameter 使用隱藏字段值

對 asp-route-parameter 使用隱藏字段值

C#
FFIVE 2023-07-09 15:25:46
我在 _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 應該從隱藏字段獲取值,并將其作為參數傳遞我嘗試使用 jQueryfunction (沒有任何運氣)像var neededValue = the value I got from another jQfunction$('#productCard').attr("asp-route-cropId", neededValue);能做到嗎?我將如何處理這個問題?謝謝!
查看完整描述

3 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

生成 html 時會消耗 razor 屬性。如果您從 javascript 引用此內容,則需要一個在頁面編譯后保留的屬性。這對于您的情況來說很棘手,因為您試圖在請求完成后具體化路線。


相反,將輸入值傳遞給操作的良好語義方式是使用表單,而不是嘗試動態構建錨點。


<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>


查看完整回答
反對 回復 2023-07-09
?
函數式編程

TA貢獻1807條經驗 獲得超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 函數:


$('.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 后,一切都很好!感謝大家的意見,給了我一些很好的線索!


查看完整回答
反對 回復 2023-07-09
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

razor 視圖中的標簽助手在服務器上執行,其結果(HTML 標記)將發送到瀏覽器,瀏覽器將呈現它。所以此時anhcor tag helper已經生成了鏈接的href屬性值。


您可以做的是,使用 JavaScript 覆蓋正常的鏈接單擊行為,從頁面讀取輸入元素值,并使用它構建新的 URL 并導航到該值。


您可以為您的 CropId 參數指定一個虛擬值。


<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;


        });


查看完整回答
反對 回復 2023-07-09
  • 3 回答
  • 0 關注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號