簡體   English   中英

使用Url.action()傳遞動態javascript值

[英]Passing dynamic javascript values using Url.action()

任何人都可以告訴我們如何使用Url.action()傳遞動態值。

就像是,

var firstname="abc";
var username = "abcd";
location.href = '@Html.Raw(@Url.Action("Display", "Customer", new { uname = firstname ,name = username}))';

firstname,用戶名未在Url.action()方法中被引用。

如何使用Url.action()傳遞這些動態值?

@Url.Action()方法是server-side @Url.Action() ,因此您不能將client-side值作為參數傳遞給此函數。 您可以使用此方法生成的server-side URL連接client-side變量,該方法是輸出中的字符串。 嘗試這樣的事情:

var firstname = "abc";
var username = "abcd";
location.href = '@Url.Action("Display", "Customer")?uname=' + firstname + '&name=' + username;

@Url.Action("Display", "Customer")server-side ,其余字符串在client-side處理,將server-side方法的結果與client-side

這個答案可能與問題沒有100%的相關性。 但它確實解決了這個問題。 我找到了實現這一要求的簡單方法。 代碼如下:

<a href="@Url.Action("Display", "Customer")?custId={{cust.Id}}"></a>

在上面的示例中, {{cust.Id}}是一個AngularJS變量。 但是,可以用JavaScript變量替換它。

我沒有嘗試使用此方法傳遞多個變量,但我希望如果需要也可以附加到Url。

就我而言,只需執行以下操作即可:

控制者:

[HttpPost]
public ActionResult DoSomething(int custNum)
{
    // Some magic code here...
}

創建沒有操作的表單:

<form id="frmSomething" method="post">
    <div>
        <!-- Some magic html here... -->
    </div>
    <button id="btnSubmit" type="submit">Submit</button>
</form>

將動作添加到表單后,設置按鈕單擊事件以觸發提交:

var frmSomething= $("#frmSomething");
var btnSubmit= $("#btnSubmit");
var custNum = 100;

btnSubmit.click(function()
    {
        frmSomething.attr("action", "/Home/DoSomething?custNum=" + custNum);

        btnSubmit.submit();
    });

希望這有助於vatos!

最簡單的方法是:

  onClick= 'location.href="/controller/action/"+paramterValue'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM