ActionLink的routeValue从一个TextBox [英] ActionLink routeValue from a TextBox
问题描述
我工作在以下方面:
1 - 用户输入文本框里面的值。
1- The user enters a value inside a textBox.
2 - 然后点击修改即可进入编辑视图。
2- then clicks edit to go to the edit view.
这是我的code:
<%= Html.TextBox("Name") %>
<%: Html.ActionLink("Edit", "Edit")%>
问题是我无法弄清楚如何从文本框中取值,并将其传递给ActionLink的,你能帮帮我吗?
The problem is I can't figure out how to take the value from the textBox and pass it to the ActionLink, can you help me?
推荐答案
您不能,除非你使用JavaScript。一个更好的方式来实现,这将是使用的一种形式,而不是一个 ActionLink的
:
You can't unless you use javascript. A better way to achieve this would be to use a form instead of an ActionLink
:
<% using (Html.BeginForm("Edit", "SomeController")) { %>
<%= Html.TextBox("Name") %>
<input type="submit" value="Edit" />
<% } %>
,它会自动发送由用户在文本框中输入到控制器动作的值:
which will automatically send the value entered by the user in the textbox to the controller action:
[HttpPost]
public ActionResult Edit(string name)
{
...
}
如果你想使用ActionLink的这里是如何你可以设置一个javascript函数将发送的值:
And if you wanted to use an ActionLink here's how you could setup a javascript function which will send the value:
<%= Html.TextBox("Name") %>
<%= Html.ActionLink("Edit", "Edit", null, new { id = "edit" })%>
和则:
$(function() {
$('#edit').click(function() {
var name = $('#Name').val();
this.href = this.href + '?name=' + encodeURIComponent(name);
});
});
这篇关于ActionLink的routeValue从一个TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!