简体   繁体   中英

href link not passing parameter value

I have a section of code in my view that displays an image and a URL on click of it. The URL is to a page on the site and directs it to that action method. I have the parameter as part of the URL, but in the Action method the parameter shows null.Hence I am redirected to the appropriate page but none of my functionalities inside the action method works.

Here is my code.

View:

<a class="lnkImage" href="@item.ImageURL" target="_blank"><img id="PrivateimgPreview" src="@item.ActualImage"/></a>

The value of @item.Image URL which I am getting from the database is http://localhost/TestProject/Home/DemoPage/b9f074fc-f86c-4866-a7b3-33e43025293f

And my action method in Home controller is

public ActionResult DemoPage(string blockId)
{
    //Some code here
    return(View);
}

This is my mapping route in Route.config

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );  

What am I missing?

I think that you need to either change 'blockId' to just 'id' or add the following route:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{blockId}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );  

I would think it makes more sense to just rename your parameter in the signature rather than creating a nearly identical route for this action method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM