簡體   English   中英

如何將JS數據從腳本文件傳遞到C#方法

[英]How to pass JS data from script file to C# method

一段時間以來,我一直在為這個問題而絞盡腦汁。 如何調用JS函數並檢索要在C#方法中使用的數據?

在此示例中我應該使用ViewBag嗎?

.cshtml

<script>
  function somefunction(str)
   {
      //use str within Index() method
      return str;
   }
</script>

調節器

   public IActionResult Index()
        {
            //use the data within this method
            return View();
        } 

你嘗試過這個嗎

在javascript函數中:

//Do your action
window.location.href = '@Url.Action("Index", new { value = 1})';

並修改索引操作以接受如下參數:

public ActionResult Index(int value)

當您的索引返回一個View時,window.location.href基本上將重定向到Index操作。

您還可以使用javascript中的字符串連接創建網址,並使用該網址進行重定向。

在我的情況下,@ Url.Action轉換為以下URL:“ / Home / Index?value = 1”。

暫無
暫無

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

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