My query SQL DOES NOT WORK

Hi, i did this simple query, where i show in my list of magazine only the one belonging to the menuid choosen in the link but it doe not work

Query :

 //I  think you can actually drop the Likes include as it will be included with the Likes.Select include, so maybe try this (although if it doesn't work, use the first example which should):
            var magazines = db.Magazines.Include(m => m.ParentMenuMag).Include(m => m.ParentSubMenuMag).Include(m => m.Likes.Select(l => l.User)) as IQueryable<Magazine>;

            if (menuid.HasValue)
            {
                magazines = magazines.Where(r => r.ParentMenuMag.MenuId.Equals(menuid.Value));
            }

Link

<a href="@Url.RouteUrl("MagazineRoute", new { controller = "Magazine", menuid = menu.MenuId, menuname = MenuName.URLFriendly() })" title="@MenuName">@MenuName<i class="fa fa-angle-down"></i></a>    

Result link

<a href="/Magazine/Index/1/styles-inspirations" title="Styles &amp; Inspirations">Styles &amp; Inspirations<i class="fa fa-angle-down"></i></a> 

RouteConfig.cs

 //localhost/Magazine/menuid/menuname/submenuid/submenuname/ID
            routes.MapRoute(
            name: "MagazineRoute",
            url: "{controller}/{action}/{menuid}/{menuname}/{submenuid}/{submenuname}/{id}",
            defaults: new { controller = "Magazine", action = "Index", menuid = UrlParameter.Optional, menuname = UrlParameter.Optional, submenuid = UrlParameter.Optional, submenuname = UrlParameter.Optional, id = UrlParameter.Optional }
           );

What is wrong?

You question was not clear for me. I didn’t understand the relation between the hyperlink and the query.

Hi,

grafic.web

does not work

It’s best to always tell what is the error or behavior you see. For example it can happen that the problem is unrelated to what you shown in which case someone will waste time in trying to find what is the error you have (that is an information that you
know already) rather than on how to solve your issue.

HI, no error.. just i see all the item of the list and not only the onewith the idmenu choose…

in fact the query  has not be called.. why? th idmenu exist

Even the behavior is helpfull. So for now it would seem that menuid.HasValue is false. Please use the VS debugger to see if this is the case. From what you
shown I don’t see why.

menuid is part of your action signature I suppose? (and so I could’t imagine that without knowing what actually happens).

Try /Magazine/Index/1/a/2/b/3 in your browser and see in the VS debugger if the various values are correctly populated etc…

The problem is that if i change my link :

<a href="@Url.RouteUrl("MagazineRoute", new { controller = "Magazine", menuid = menu.MenuId, menuname = MenuName.URLFriendly() })" title="@MenuName">@MenuName<i class="fa fa-angle-down"></i></a>    

like this :

@Html.RouteLink(MenuName, "MagazineRoute", new { controller = "Magazine", menuid = menu.MenuId, menuname = MenuName.URLFriendly() })</li>

it works perfectly… so the problem, i guess is in the  @Url.RouteUrl(", becaus if i use the
@Html.RouteLink( the query can find the menuid.HasValue

How comes?

What if using "view source" in both cases to see which HTML markup is generated and see how they differ?

Leave a Reply