如何将文本框的值从锚标记传递到asp.net mvc中的控制器方法

时间:2021-09-11 09:49:10

I want to pass value of textbox from anchor tag to controller method, basically i want to transfer it from javascript function to the controller method. I am not able to understand how to transfer the value to search and current page to controller. Below is a code snippet please help to solve it.. thanks..

我想将文本框的值从锚标记传递给控制器​​方法,基本上我想将它从javascript函数传递给控制器​​方法。我无法理解如何将值传输到搜索和当前页面到控制器。下面是一个代码片段请帮忙解决..谢谢..

<script>
  $(document).ready(function () {
            $('#Button1').click(function () {
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: 'Home/getUsersList',
                data: "{'searchString':'" + document.getElementById('searchString').value + "'}",
                    async: false,
                    success: function (response) {
                        $('#showData').html(response)
                    },
                    error: function () { alert("error"); }
                });
            });
        });
</script>

<body>
    @Html.TextBox("searchString")
</body>

<a class="@(p == ViewBag.CurrentPage ? "current" : "")" href="@Url.Action("getUsersList", "Home", new { page = p })">@p</a>

[HttpPost]
public ActionResult getUsersList(string searchString)
 {
         int page=1;
 }

3 个解决方案

#1


0  

try this instead of @Html.TextBox("searchString")

试试这个而不是@Html.TextBox(“searchString”)

<input type="text" id="searchString" value="@zyz" />

and simply do this

并简单地这样做

$('#searchString').val()

#2


0  

At first I think your URL parameter is wrong, make it look like this so MVC will handle it using the routing table. Add the dataType parameter as well.

起初我认为你的URL参数是错误的,使它看起来像这样,所以MVC将使用路由表处理它。同时添加dataType参数。

...
url: '@Url.Action("GetUserList", "Home")',
dataType: "json",
...

If it doesn't solve it, post more information about your problem (e.g. the request status (404?) or any .NET errors)

如果它没有解决它,请发布有关您的问题的更多信息(例如请求状态(404?)或任何.NET错误)

#3


0  

in data section pass this with search string name.

在数据部分用搜索字符串名称传递它。

    <script>
  $(document).ready(function () {
            $('#Button1').click(function () {
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: 'Home/getUsersList',
                data: "{'searchString':'" + $('#searchString').val() + "'}",
                    async: false,
                    success: function (response) {
                        $('#showData').html(response)
                    },
                    error: function () { alert("error"); }
                });
            });
        });
</script>

i think this will work for you or firstly save the value of text box in other variable and then pass in data and then recieve in getUsersList controller method

我认为这对你有用或者首先将文本框的值保存在其他变量中然后传入数据然后接收到getUsersList控制器方法

#1


0  

try this instead of @Html.TextBox("searchString")

试试这个而不是@Html.TextBox(“searchString”)

<input type="text" id="searchString" value="@zyz" />

and simply do this

并简单地这样做

$('#searchString').val()

#2


0  

At first I think your URL parameter is wrong, make it look like this so MVC will handle it using the routing table. Add the dataType parameter as well.

起初我认为你的URL参数是错误的,使它看起来像这样,所以MVC将使用路由表处理它。同时添加dataType参数。

...
url: '@Url.Action("GetUserList", "Home")',
dataType: "json",
...

If it doesn't solve it, post more information about your problem (e.g. the request status (404?) or any .NET errors)

如果它没有解决它,请发布有关您的问题的更多信息(例如请求状态(404?)或任何.NET错误)

#3


0  

in data section pass this with search string name.

在数据部分用搜索字符串名称传递它。

    <script>
  $(document).ready(function () {
            $('#Button1').click(function () {
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: 'Home/getUsersList',
                data: "{'searchString':'" + $('#searchString').val() + "'}",
                    async: false,
                    success: function (response) {
                        $('#showData').html(response)
                    },
                    error: function () { alert("error"); }
                });
            });
        });
</script>

i think this will work for you or firstly save the value of text box in other variable and then pass in data and then recieve in getUsersList controller method

我认为这对你有用或者首先将文本框的值保存在其他变量中然后传入数据然后接收到getUsersList控制器方法