1

I am having a < href > attributes in my .cshtml page at mvc4 @cp.Name in my mvc 4 .... what i need is if a person clicks the above link . i have to redirect him to any of the ActionName in Controller (for eg: Index in HomeController )...... how to do it. In my above sample i have redirected to google.com...... but i need to redirect to any of actionname in controller...... My code:

<nav> @{ List<MenuRazor.Models.MenuItem> menulist = ViewBag.Menu; } 
<ul id="menu">  
    @foreach (var mp in menulist.Where(p => p.ParentMenu_Id == 0)) {
    <li> <a href="#">@mp.Name</a> 
        @if (menulist.Count(p => p.ParentMenu_Id == mp.Id) > 0)
        { @:<ul> }  
          @RenderMenuItem(menulist, mp)  
          @if (menulist.Count(p => p.ParentMenu_Id == mp.Id) > 0){@:</ul> } 
    </li> } 
</ul> 

@helper RenderMenuItem(List<MenuRazor.Models.MenuItem> menuList, MenuRazor.Models.MenuItem mi) 
{
    foreach (var cp in menuList.Where(p => p.ParentMenu_Id == mi.Id)) {
        @:<li> <a href="http://codeproject.com">@cp.Name</a> 

        if (menuList.Count(p => p.ParentMenu_Id == cp.Id) > 0) { 
            @:<ul> 
        } 
        @RenderMenuItem(menuList, cp) 
        if (menuList.Count(p => p.ParentMenu_Id == cp.Id) > 0) { 
           @:</ul> 
        } else  { 
           @:</li> 
        }  
} }   </nav>

2 Answers 2

1

You can use: @Url.Action("ActionName", "ControllerName")

Refer this MSDN link for more information.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use @Html.ActionLink or @Url.Action to generated you link.

@Html.ActionLink("Link name", "Action", "Controller", new { id = your_param }, null)

or

@Url.Action("Action", "Controller")

The @Html.ActionLink might better suit your current problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.