ActionLink routeValue from a TextBox

后端 未结 1 1319
鱼传尺愫
鱼传尺愫 2020-12-10 21:55

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

相关标签:
1条回答
  • 2020-12-10 22:07

    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);
        });
    });
    
    0 讨论(0)
提交回复
热议问题