Monday, March 30, 2015

MVC area

You have to add area to every action link otherwise link will be messed up.

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new {area = "", id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { area = "",title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new {area = "", id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { area = "", id = "loginLink" })</li>
    </ul>
}


Linking Between Two Areas

ASP.NET MVC Views often need to link to some action method residing in controller classes. Unless otherwise specified, action methods and controllers are assumed to be from the current area. In case you wish to link to an action method belonging to some other area, here is how you would do just that:
  1. <h3>You are inside main area. To navigate to some other area please click on the following links</h3>
  2. <%= Html.ActionLink("Main Area", "Index", "Home", new { area = "" }, null)%>
  3. <br /><br />
  4. <%= Html.ActionLink("Blog Area", "Index", "Home", new { area = "Blog" }, null)%>
  5. <br /><br />
  6. <%= Html.ActionLink("Help Desk Area", "Index", "Home", new { area = "HelpDesk" }, null)%>
  7. <br /><br />
  8. <%= Html.ActionLink("Shopping Area", "Index", "Home", new { area = "Shopping" }, null)%>
The above fragment is from the Index view of the main area. It renders four hyperlinks using ActionLink(). Notice the fourth parameter that specifies area name. This parameter governs the area whose action method is being linked to.

Redirecting to an Action Belonging to Another Area

You can also redirect to some action method of a specific area using RedirectToAction() method. Consider the following action method:
  1. public ActionResult Index()
  2. {
  3. return RedirectToAction("Index", "Home", new { Area = "HelpDesk" });
  4. }
As you can see, the Index() action method of the main area redirects the control to Index() action method of the HelpDesk area.


http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC

http://www.itorian.com/2013/10/area-in-mvc-5-with-example-step-by-step.html

http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC

http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm


No comments:

Post a Comment