微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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()是一个不存在的客户端方法在那一点 – 即它在当前的背景下不存在

原文地址:https://www.jb51.cc/aspnet/251601.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐