简体   繁体   English

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

[英]Getting Value from textbox inside partial View in asp.net mvc

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 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 HomeController.cs

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

        [HttpGet]
        public ActionResult UsersList(string str)
        {

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

If you use a link to call the action it will result in a HttpGet 如果您使用链接来调用操作,则会导致HttpGet

ViewUser is decorated with HttpPost. ViewUser用HttpPost装饰。 You need to drop that attribute. 您需要删除该属性。

if you are just wanting to redirect you could also just do a window.location instead of setting the source of the link. 如果您只想重定向,也可以执行window.location而不是设置链接的源。 change the href of the source to "#" and for your click event 将来源的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. 我相信您需要将p放在引号中,否则它将认为它是一个变量。 If you want p passed that should work fine. 如果您想通过p,应该可以正常工作。 looking at your code though it looks like you want the p that is specific to that link to be passed with it. 查看您的代码,尽管看起来您希望将特定于该链接的p与它一起传递。 For that I would recommend putting p as the id on the link. 为此,我建议将p作为id放在链接上。 Then in your script you should be able to do something like 然后在您的脚本中,您应该可以执行以下操作

$(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. 要获得p的值,您需要将其放在replace中,以使其包含在链接中。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。 I would also suggest you don't use p as the value to replace on. 我还建议您不要将p用作替换的值。 If there are any p's in the link they will be replaced by the value. 如果链接中有任何p,则将其替换为该值。 So use something unique. 因此,请使用独特的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET MVC 中无法从视图中获取文本框值到 controller - Trouble getting textbox value from view to controller in ASP.NET MVC 在提交点击时从asp.net mvc文本框获取价值 - Getting Value from an asp.net mvc textbox on submit click 从控制器获取价值以在ASP.NET MVC中进行查看 - Getting value from controller to view in ASP.NET MVC 如何在Asp.net MVC中将值从部分视图传递到共享视图_Layout.cshtml? - How to pass value from partial view to shared view _Layout.cshtml in asp.net MVC? 单击ASP.NET从带按钮的DetailsViews内部获取文本框值; 文本框数据独立于绑定数据 - ASP.NET getting textbox value from inside a DetailsViews with a button is clicked; that textbox data is independent of the binded data asp.net mvc 从视图中的文本框获取数据返回到存储过程以编辑记录 - asp.net mvc getting data from textbox on view back to stored procedure to edit record 从ASP.NET MVC Partial View中调用JavaScript - Call JavaScript from within an ASP.NET MVC Partial View 从部分视图(ASP.NET MVC)创建模态窗口 - Modal window create from Partial View (ASP.NET MVC) Asp.net MVC-从区域渲染局部视图 - Asp.net MVC - Render a partial View From an Area 在部分视图中从 ASP.Net MVC 5 中的浏览器捕获 URL - Capture URL from browser in ASP.Net MVC 5 in partial view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM