ActionLink routeValue from a TextBox

末鹿安然 提交于 2019-12-17 19:54:31

问题


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?


回答1:


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);
    });
});


来源:https://stackoverflow.com/questions/5838273/actionlink-routevalue-from-a-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!