The_Outsider The_Outsider - 14 days ago 5
C# Question

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?

Answer

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.

Comments