How to get textbox value in Action in asp.net MVC

2019-09-12 13:50发布

I want to send the value of textbox to the Action Method for searching the technology for that i want to get the value of textbox in Action.

I have the following code :-

@Html.TextBox("technologyNameBox", "", new { id = "technologyName", @class = "form-control", @placeholder = "Search For Technology" })

<span class="input-group-btn" style="text-align:left">
      <a class="btn btn-default" id="searchTechnology" 
         href="@Url.Action("SearchTechnology", "Technology",
         new {technologyName="technologyName",projectId=ProjectId })">
           <span class="glyphicon glyphicon-search "></span>
      </a>
</span>

Question :- How to get the value of textbox "technologyNameBox" in Action ?

Please help me out. Thanks in Advance!

1条回答
对你真心纯属浪费
2楼-- · 2019-09-12 14:41

You'd have to append the value to the URL via JavaScript before directing the user. Using jQuery (since that generally comes packaged with ASP.NET), it might look something like this (with a good bit of manual conditional checks for blank values or query string parameters):

$('#searchTechnology').click(function (e) {
    e.preventDefault();

    var url = '@Url.Action("SearchTechnology", "Technology", new { projectId=ProjectId })';
    var technologyName = $('#technologyName').val();

    if (technologyName.length < 1) {
        // no value was entered, don't modify the url
        window.location.href = url;
    } else {
        // a value was entered, add it to the url
        if (url.indexOf('?') >= 0) {
            // this is not the first query string parameter
            window.location.href = url + '&technologyName=' + technologyName;
        } else {
            // this is the first query string parameter
            window.location.href = url + '?technologyName=' + technologyName;
        }
    }

    return false;
});

The idea is that when the user clicks that link, you would fetch the value entered in the input and append it to the URL as a query string parameter. Then redirect the user to the new modified URL.

查看更多
登录 后发表回答