Skip to content

Instantly share code, notes, and snippets.

@chaim1221
Created April 29, 2016 04:21
Show Gist options
  • Save chaim1221/3c0b6ee01c74a56fcb54918fac08cf86 to your computer and use it in GitHub Desktop.
Save chaim1221/3c0b6ee01c74a56fcb54918fac08cf86 to your computer and use it in GitHub Desktop.

One possible pattern for using tabs:

// _ViewStart.cshtml
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
// _Layout.cshtml
@using MyApp.Helpers
<!DOCTYPE html> <!--etc-->
    <div id="menucontainer">
        <ul id="menu">
            <li></li> <!-- for appearance... -->
            <li>
                @Html.ActionLink(@MyApp.Resources.Shared.persontab, "Index", "Professor", null, new { id = "menulinkperson" })
            </li>
            <li>
                @Html.ActionLink(@Machete.Web.Resources.Shared.activitytab, "Index", "Student", null, new { id = "menulinkactivity" })
        </ul>
    </div>
<!--etc-->

Then, in your view(s)...

@model MyApp.Professor
@using MyApp.Helpers
@{ ViewBag.Title = "Professors" }

@Html.DisplayForModel()

That has to be defined in /Views/Shared/DisplayTemplates but I believe [correct me if I'm wrong] that ASP.NET picks those up by naming convention so that the Person.cshtml DisplayTemplate will be loaded here.

// /Views/Shared/DisplayTemplates/Professor.cshtml
@model MyApp.Professor
<table class="table">
    <tr>
        <th>@Html.LabelFor(model => model.FirstName)</th>
        <th>@Html.LabelFor(model => model.LastName)</th>
    </tr>
    @foreach (var item in Model) 
    {
        <tr>
            <td>@item.FirstName</td>
            <td>@item.LastName</td>
            <!--etc-->
        </tr>
    }
</table>

Finally, a controller...

namespace MyApp.Controllers
{
    public class ProfessorController : Controller
    {    
        public ActionResult Index()
        {
            return View();
        }
    
    // Other ActionResults can return PartialView() 
    // so you don't have to refresh/rewrite the whole thing
    }
}

Then you can do the same for the Student model...

Basically what I'm saying here is that there's a way to create a template that uses tabs outside of the ASP.NET View engine, and then to invoke the views from that engine using naming conventions.

Here's a plethora of information on the MVC pattern in ASP.NET:

http://docs.asp.net/en/latest/mvc/index.html

Let me know if that helps, or if you need clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment