gpt4 book ai didi

javascript - 如何将 JS 数据从脚本文件传递到 C# 方法

转载 作者:行者123 更新时间:2023-12-02 23:02:47 25 4
gpt4 key购买 nike

我为这个问题绞尽脑汁有一段时间了。如何调用 JS 函数并检索要在 C# 方法中使用的数据?

在此示例中我应该使用 ViewBag 吗?

.cshtml

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

Controller

   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 基本上会重定向到索引操作。

您还可以使用 JavaScript 中的字符串连接创建一个 url,并使用该 url 进行重定向。

在我的例子中,@Url.Action 转换为以下 URL:“/Home/Index?value=1”。

关于javascript - 如何将 JS 数据从脚本文件传递到 C# 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com