Murugappan
Murugappan

Reputation: 87

How to use href attribute in mvc4

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>

Upvotes: 1

Views: 5772

Answers (2)

Huske
Huske

Reputation: 9296

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.

Upvotes: 0

Pradeep
Pradeep

Reputation: 3276

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

Refer this MSDN link for more information.

Upvotes: 1

Related Questions