asp.net-mvc – 使用actionlink将文本框的值从视图传递到控制器

在我的表格中,我有一个文本框如下
@Html.TextBox("first_name")

我需要通过actionlink将此文本框的值传递给控制器​​.

我试过以下

@Html.ActionLink("View","view_Details",new { name = first_name})

但这是错误

“first_name” does not exist in the current context

这可能使用Actionlink吗?

我的控制器签名是

public ActionResult view_Details(string name)
     {
            return View();
     }

编辑

@Html.ActionLink("View",new { name = getname()})

 <script type="text/javascript">
      function getname() {
           return $("#first_name").val();
       }
</script>

我试过上面的代码.它也给出了错误

getname() does not exist in the current context

解决方法

您需要javascript / jquery来获取文本框的值,然后更新要重定向到的URL

HTML

@Html.ActionLink("View",new { id = "myLink" }) // add id attribute

脚本

$('#myLink').click(function() {
  var firstname = $('#first_name').val(); // get the textBox value
  var url = $(this).attr('href') + '?name=' + firstname; // build new url
  location.href = url; // redirect
  return false; // cancel default redirect
});

旁注:进一步编辑,你收到该错误的原因是剃刀代码(@ Html.ActionLink()在发送到视图之前在服务器上解析,但getname()是一个不存在的客户端方法在那一点 – 即它在当前的背景下不存在

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....