ActionLink routeValue from a TextBox
10,104
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)
{
...
}
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" })%>
and then:
$(function() {
$('#edit').click(function() {
var name = $('#Name').val();
this.href = this.href + '?name=' + encodeURIComponent(name);
});
});
Author by
Lisa
Updated on June 04, 2022Comments
-
Lisa almost 2 years
I'm working on the following:
1- The user enters a value inside a textBox.
2- then clicks edit to go to the edit view.
This is my code:
<%= Html.TextBox("Name") %> <%: Html.ActionLink("Edit", "Edit")%>
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?
-
Lisa about 13 yearsThat would route to /SomeController/Edit without the name value
-
Darin Dimitrov about 13 years@Shaza, yes, exactly. The same as the ActionLink. You just need to specify the controller on which this Edit action is defined.
-
Lisa about 13 yearsHow can I route to /someController/Edit/Sam ?
-
Darin Dimitrov about 13 years@Shaza, you can't using the form. You could adapt the javascript version I showed and instead of appending the name to the query string append it as route parameter:
this.href = this.href + '/' + encodeURIComponent(name);
. -
Lisa about 13 yearsWhen the function gets called?