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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 02:53:29

问题


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:


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.



来源:https://stackoverflow.com/questions/34994378/how-to-get-textbox-value-in-action-in-asp-net-mvc-5

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