Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

asp.net - ActionLink routeValue from a TextBox

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...