1

I'm beginner in web development. I'm stuck in passing dynamic value to another page when it was clicked. The only thing I need is to get clicked hyperlink's value in another page. Please help me with this.

First page:

@for (int j = 0; j < listOfUrls.Count; j++)
{
    <a style="background: #fff url(@listOfUrls[j]) no-repeat center;"
       href="nextPage" 
       onclick="@*Pass clicked listOfUrls[j]*@" ></a>
}

Next page:

<p>@*Here I need to get the clicked value of hyperlink from previous page*@</p>

5
  • The previous page has been gone for a while ago, it simply doesn't exist. Commented Apr 4, 2018 at 9:28
  • You may try use query string to pass value Commented Apr 4, 2018 at 9:55
  • Hmm... maybe I've slightly misunderstood the question. If you want to pass something from the current page to the next page, that really can be done by the query string like Den already has suggested. Commented Apr 4, 2018 at 10:41
  • I've already using Sessions to pass a value. But when I do so, it's passing only the last item of the listOfUrls. That's why I was thinking using of OnClick to handle the CLICKED index and pass the value of that index. Commented Apr 4, 2018 at 11:01
  • Or maybe there is another way to handle the index of clicked dynamic hyperlink... Commented Apr 4, 2018 at 11:03

1 Answer 1

1

I have created a simple controller for showing demo.

Demo Controller

From this Controller, I am sending the collection to View.

public class DemoController : Controller
{
    // GET: Demo
    public ActionResult Index()
    {
        List<DemoModel> list = new List<DemoModel>()
        {
            new DemoModel {Id = "1", Link = "One"},
            new DemoModel {Id = "2", Link = "Two"},
            new DemoModel {Id = "3", Link = "Three"},
            new DemoModel {Id = "4", Link = "Four"}
        };

        return View(list);
    }
}

Values which I am going to send to Next page is ID in that I am sending the value of a link.

@model  List<WebApplication9.Models.DemoModel>
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">Panel Heading</div>
            <div class="panel-body">

                @for (int i = 0; i < Model.Count; i++)
               {
                    <a href="@Url.Action("Index", "Demo2", new {@id = Model[i].Link})">
                        #Link to Page @Model[i].Link
                    </a>

                    <br />
                }
            </div>
        </div>
    </div>
</body>
</html>

Rendering View

enter image description here

We are Sending values to Demo2Controller in which it has Index Action Method which take ID as input.

public class Demo2Controller : Controller
{
    // GET: Demo2
    public ActionResult Index(string id)
    {
        if (!string.IsNullOrEmpty(id))
        {
            TempData["message"] = id;
        }
        else
        {
            TempData["message"] = "Not Clicked";
        }

        return View();
    }
}

Index View (Demo2)

    @{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
    <div class="alert alert-success">
        <strong>Success!</strong> @TempData["message"]
    </div>
</body>
</html>

enter image description here

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.