Question

Hi i have view called Apps, on which when user clicks it should go to info page and display records present in Apps table based on ID. For this i wrote on Info controller :

public ActionResult Info(short id)
    {

        var inf = db.Apps.Where(x => x.ID == id).FirstOrDefault();
        return View(inf);

Now i need to pass this id in Apps page. I know how to use html.actionlink. For example like this :

@Html.ActionLink("Info", "Info", new { id = item.ID })

But in my case i need to pass ID for this code which is in Apps page:

<div class="listPageCol"  onclick="location.href='Info'">

Here after onlcick function i tried to pass id by using new but its not working.As you can see, above code redirect to Info page but not taking ID. so can someone tell me how can pass id to above code?

Was it helpful?

Solution

You could use the Url.Action helper to generate the proper location:

<div class="listPageCol"  onclick="window.location.href='@Url.Action("Info", new { id = item.Id })';">

But IMHO using the @Html.ActionLink helper to generate an anchor pointing to your controller action and not relying on any javascript is semantically more correct approach:

@Html.ActionLink("Info", "Info", new { id = item.ID })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top