String parameter in url

Hy Guys, I need to pass a string parameter to the action method because I want to implement tag search on my site using asp.net MVC, but every time in the action, it gets passed a null value.

I am posting the code!

I am trying to create a personal itinerary.

 routes.MapRoute(
            "TagsRoute",
            "Tags/PostList/{tag}",
            new {tag = "" }
        );

      

My RouteLink on the browse page for each tag:

<% foreach (var itemtags in item.tblTagArt) {%> 

    <%= Html.RouteLink(itemtags.Tags.TagName,"TagsRoute", 
        new {tag=itemtags.Tags.TagName})%>,    
<% } %>

      

My method action:

 public ActionResult PostList(string tag)
    {
        if (tag == "")
        {
            return RedirectToAction("Index", "Home");
        }
        else
        {
            var articoli = artdb.GetArticoliByTag(tag);

            if (articoli == null)
            {
              return RedirectToAction("Index", "Home");
            }

            return View(articoli);
        }
    }

      

The problem is the value is always null and therefore var articoli is always empty!

Probably my problem is the tag I have to make for the route associated with my tag parameter.

Can anyone help me?

NB I am using ASP.NET MVC 1.0, not 2.0!

+2


a source to share


1 answer


Where did you install this custom route definition? It should be up Default

.



public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "TagsRoute",
        "Tags/PostList/{tag}",
        new { controller = "Tags", action = "PostList", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Users", action = "Index", id = UrlParameter.Optional }
    );
}

      

+4


a source







All Articles