0

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)

  <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");
        }
7
  • see my answer here stackoverflow.com/questions/20662629/… Commented Jan 3, 2014 at 15:18
  • Will that work if i have my anchor link inside partial view and textbox in main view... Commented Jan 3, 2014 at 15:24
  • as long as the partial view has been loaded when the script runs it should work fine Commented Jan 3, 2014 at 15:32
  • I have edited my code. It is passing null value inside controller. Please see where i am wrong. Commented Jan 3, 2014 at 15:56
  • since you are setting the source on the link click, if I had to guess, the link is being fired and then the source is being changed. try changing the href on the link to "" to see if my guess is correct. Commented Jan 3, 2014 at 15:59

2 Answers 2

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

$('.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

$(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.

Sign up to request clarification or add additional context in comments.

3 Comments

After Changing from $('.linkAction').attr('src') to .attr('href') is working. But i want to ask one more point if i want to pass two parameters so that one remains the same and alter only one parameter through javascript, how can that be achieved. Thanks for the answer.
You can put as many parameters in the url.action as you want. Just separate them with a comma. If the second won't change you may not need to replace but just put it in the list
I have passed another parameter <a href="@Url.Action("UsersList", "Home", new { page=p,str = "---" })" class="linkAction">@p</a> page=p but p is unaccessible inside javascript.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.