0

I have a table in my database called Programs. I want to display a tab for each program .I am trying to create a partial view to do this and then I want to include the partial view to every view that will need to have those tabs. My partial view looks like below.

<div id="tabs">

    <ul>
        <li id="HomeTab">@Html.ActionLink("Dashboard", "Index", "Home")</li>
        <li id="Program1Tab">@Html.ActionLink("Program1", "Index", "Program")</li>
        <li id="Program2Tab">@Html.ActionLink("Program2", "Index", "Program")</li>
    </ul>
</div>

I am hoping to dynamically create the tabs using something like

  @foreach (var ptype in Model)
    {
       <li id=\"@ptype.Name\"Tab>@Html.ActionLink(ptype.Name, "Index", "Project")</li>
    }

however I am wondering how I can load the tabs without using a controller. Can I use a helper class/method to directly access the model bypassing the controller?

update: I also tried by creating a helper method

namespace MyProject.Helpers
{
    public class ProgramTypes
    {
        public static List<ProgramType> ProgramTypesList()
        {
            MyDbContext db = new myDbContext();
            return db.ProgramTypes.ToList<Programtype>();

        }
    }
}

and then accessing them using

 @foreach (var ptype in MyProject.Helpers.ProgramTypes.ProgramTypesList())

however i am not sure if this is the correct thing to do.

3
  • just added some more code.see above Commented Jun 20, 2012 at 18:15
  • Did you look at editor templates ? Commented Jun 21, 2012 at 0:03
  • yes. I have used Editor Templates. They are still"Views" and have no access to the model directly. Commented Jun 21, 2012 at 18:34

1 Answer 1

1

The best solution is - passing collection of programs to your view

@model IQueyrable<Program>
<div id="tabs">
    <ul>
    @foreach (var ptype in Model)
    {
       <li>@Html.RenderPartial("ProgramTab", ptype)</li>
    }
    </ul>
</div>

so you have to create another partial view where you will display program details. If you want organize this like tabs you should use something like jquery tabs

you don't have to use actionlink just RenderPartial or RenderAction

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

3 Comments

you don't have to use actionlink just renderpartial or renderaction
great idea to user RenderAction. Thank you @Bohdan
sort of though noone anwered my original question if what I was doing was wrong or not

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.