加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:10:21 所属栏目:asp.Net 来源:网络整理
导读:在我的表格中,我有一个文本框如下 @Html.TextBox("first_name") 我需要通过actionlink将此文本框的值传递给控制器??. 我试过以下 @Html.ActionLink("View","view_Details",new { name = first_name}) 但这是错误的 “first_name” does not exist in the cur
在我的表格中,我有一个文本框如下
@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()是一个不存在的客户端方法在那一点 – 即它在当前的背景下不存在

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读