从asp.net mvc中的部分视图中的文本框获取值

时间:2022-12-02 11:38:10

I want to get the value of textbox inside partial view inside the anchor link tag so that i can pass the value to the controller from the anchor link tag every time i click on the link. How can i perform this action. Please give some suggestion.. Thanks...

我想在锚链接标记内的局部视图中获取文本框的值,以便每次单击链接时我都可以从锚链接标记将值传递给控制器​​。我该如何执行此操作。请给出一些建议..谢谢......

ViewUser.cshtml

      @model IEnumerable<Search_Paging.Models.tbl_product>
<html>
<head>
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.linkAction').click(function (e) {
                $('.linkAction').attr('href', '@Url.Action("UsersList", "Home", new { str = "---" })'.replace("---", $('.txtString').val()));
            });
            e.preventDefault();
        });
    </script>
</head>
<body>
    @Html.TextBox("Strings", null, new { @class = "txtString" })
    <input type="button" value="filter" id="Button1" />
    <div class="pagination">
        @for (int p = 1; p <= ViewBag.Total; p++)
        {
            <a href="@Url.Action("UsersList", "Home", new { str = "---" })" class="linkAction">@p</a>
        }
    </div>
</body>
</html>

getUsersList.cshtml (Partial View)

getUsersList.cshtml(部分视图)

  <h2>@ViewBag.strings</h2>

HomeController.cs

        [HttpGet]
        public ActionResult ViewUser()
        {
            ViewBag.Total = 15;
            return View();
        }

        [HttpGet]
        public ActionResult UsersList(string str)
        {

            ViewBag.strings = str;
            return PartialView("UsersList");
        }

2 个解决方案

#1


0  

if you are just wanting to redirect you could also just do a window.location instead of setting the source of the link. change the href of the source to "#" and for your click event

如果你只是想重定向,你也可以只做一个window.location,而不是设置链接的来源。将源的href更改为“#”和您的click事件

$('.linkAction').click(function () {
    alert("link clicked");
    window.location = '@Url.Action("getUsersList", "Home", new { page = "----" })'.replace("----", $('.txtString').val());

});

Edit:

I believe you need to put the p in quotes or it will think it is a variable. If you want p passed that should work fine. looking at your code though it looks like you want the p that is specific to that link to be passed with it. For that I would recommend putting p as the id on the link. Then in your script you should be able to do something like

我相信你需要把p放在引号中,否则它会认为它是一个变量。如果你想要传递p应该工作正常。查看您的代码虽然看起来您希望将特定于该链接的p与其一起传递。为此,我建议将p作为id作为链接。然后在你的脚本中你应该能够做类似的事情

$(this).id;

to get the value of p, you will need to put that in the replace for it to be included in the link. Let me know if you have any questions. I would also suggest you don't use p as the value to replace on. If there are any p's in the link they will be replaced by the value. So use something unique.

要获得p的值,您需要将其放在替换中以使其包含在链接中。如果您有任何疑问,请告诉我。我还建议你不要用p作为替换值。如果链接中有任何p,则它们将被值替换。所以使用独特的东西。

#2


0  

If you use a link to call the action it will result in a HttpGet

如果您使用链接来调用操作,它将导致HttpGet

ViewUser is decorated with HttpPost. You need to drop that attribute.

ViewUser用HttpPost修饰。您需要删除该属性。

#1


0  

if you are just wanting to redirect you could also just do a window.location instead of setting the source of the link. change the href of the source to "#" and for your click event

如果你只是想重定向,你也可以只做一个window.location,而不是设置链接的来源。将源的href更改为“#”和您的click事件

$('.linkAction').click(function () {
    alert("link clicked");
    window.location = '@Url.Action("getUsersList", "Home", new { page = "----" })'.replace("----", $('.txtString').val());

});

Edit:

I believe you need to put the p in quotes or it will think it is a variable. If you want p passed that should work fine. looking at your code though it looks like you want the p that is specific to that link to be passed with it. For that I would recommend putting p as the id on the link. Then in your script you should be able to do something like

我相信你需要把p放在引号中,否则它会认为它是一个变量。如果你想要传递p应该工作正常。查看您的代码虽然看起来您希望将特定于该链接的p与其一起传递。为此,我建议将p作为id作为链接。然后在你的脚本中你应该能够做类似的事情

$(this).id;

to get the value of p, you will need to put that in the replace for it to be included in the link. Let me know if you have any questions. I would also suggest you don't use p as the value to replace on. If there are any p's in the link they will be replaced by the value. So use something unique.

要获得p的值,您需要将其放在替换中以使其包含在链接中。如果您有任何疑问,请告诉我。我还建议你不要用p作为替换值。如果链接中有任何p,则它们将被值替换。所以使用独特的东西。

#2


0  

If you use a link to call the action it will result in a HttpGet

如果您使用链接来调用操作,它将导致HttpGet

ViewUser is decorated with HttpPost. You need to drop that attribute.

ViewUser用HttpPost修饰。您需要删除该属性。