2017-06-30 1 views
1

Angenommen, ich wiederhole alle eingeschränkten Controller und ihre zugehörigen Aktionen und füge diese Controller und Aktionen in meine Klasse ein.Wie werden Controller und Aktionen in der Liste angezeigt?

public class ControllerActionList 
{ 
    public string ControllerName { get; set; } 
    public string ActionName { get; set; }   
} 

So kann ich mit der Hand zu Simulationszwecken zu füllen:

public List<ControllerActionList> ControllerActionList() 
{ 
    List<ControllerActionList> oControllerActionList = new List<ControllerActionList> 
    { 
     new ControllerActionList { ControllerName = "Home", ActionName = "Index" }, 
     new ControllerActionList { ControllerName = "Home", ActionName = "DashBoard" }, 
     new ControllerActionList { ControllerName = "Home", ActionName = "Chart" }, 

     new ControllerActionList { ControllerName = "HR", ActionName = "Leave" }, 
     new ControllerActionList { ControllerName = "HR", ActionName = "Loan" }, 
     new ControllerActionList { ControllerName = "HR", ActionName = "NoticeBoard" }, 

     new ControllerActionList { ControllerName = "PayRoll", ActionName = "View" }, 
     new ControllerActionList { ControllerName = "PayRoll", ActionName = "Edit" }, 
     new ControllerActionList { ControllerName = "PayRoll", ActionName = "Process" } 
    }; 

    return oControllerActionList; 
} 

nun, was Logik soll ich diese Steuerungen und ihre zugehörigen Aktionsnamen in meiner Ansicht angezeigt werden verwenden, um (siehe Bilder unten)?

Siehe, die Controller-Namen sind Home, HR und Payroll und Aktionen werden unten angezeigt.

Können Sie mir sagen, wie der Controller-Name zuerst als Kopfzeile und dann als Aktionsname angezeigt wird? In meinem Fall wird der Name des Controllers in der Klasse wiederholt.

Eine Sache, die ich tun kann, ist, dass ich eine for-Schleife schreiben kann und wenn Controller Name ändert, werde ich eine neue div erstellen, wo ein neuer oder nächster Controller-Name angezeigt wird.

Bitte geben Sie mir auch Hinweise, wie Sie dieses Design über implementieren.

Meine Fragen sind nun:

1) Wie ein Controller-Namen an der Spitze und die damit verbundene Aktionen unten in einem div zeigen, und die Aktion für jeden Controller wiederholen?

2) Wie man das gezeigte Design mit Bootstrap erreicht?

enter image description here

+4

Verwenden GroupBy auf controller und dann können Sie durchlaufen die Aktionsnamen auf dieser Grundlage und abflachen – Nkosi

+0

ich bin Ich bin mir nicht sicher, wie man die Gruppierung nach dem Namen des Controllers benutzt, weil ich den Namen aller Aktionen für jeden Controller bekommen muss. können Sie ein Beispiel nur für einen Hinweis bereitstellen, um Gruppe für Controller-Name zu verwenden. –

+0

Linq GroupBy ist gut dokumentiert –

Antwort

2

Ich habe diese Antwort majorly neu geschrieben, werfen Sie einen Blick auf die Geschichte ändern, wenn Sie das alte und schlecht sehen wollen.

Wie Sie in den Kommentaren gesehen haben, sagte die Mehrheit, Sie müssen .GroupBy verwenden, um die einzige ControllerActionItems zu bekommen. Ich habe das jetzt in zwei Klassen umgeschrieben, eine Klasse namens ControllerBlock und eine ControllerAction.

ControllerBlock (hat nun eine 1-n-Beziehung mit ControllerAction):

public class ControllerBlock 
{ 
    public string ControllerName { get; set; } 
    public List<ControllerAction> ControllerActions { get; set; } 
} 

ControllerAction:

public class ControllerAction 
{ 
    public string ActionName { get; set; } 
    public bool ActionActive { get; set; } 
} 

Unter der Annahme, dass diese Rechtecke sind in der Tat CheckBox Kontrollen, Sie kann eine so genannte Editor-Vorlage verwenden, um Code zu reduzieren und Probleme in Ihrem Controller zu minimieren.

Je nachdem, wo Sie den Code verwenden möchten, können Sie ihn in Views eingeben ->Shared ->EditorTemplates.

Erstellen Sie nun eine Ansicht, die nach dem Modell genau benannt ist, hier: ControllerBlock

EditorTemplate structure in solution

Öffnen Sie die ControllerBlock.cshtml Datei und verweisen das Modell über @model ControllerBlock, dann bauen Sie Ihre :

@model ControllerBlock 

<div class="col-lg-12 panel"> 
    @Model.ControllerName 
    <hr class="row"> 
    @foreach (ControllerAction actionItem in Model.ControllerActions) 
    { 
     <div class="col-lg-4">@Html.CheckBoxFor(m => actionItem.ActionActive)<a href="~/@Model.ControllerName/@actionItem.ActionName">@actionItem.ActionName</a></div> 
    } 
</div> 

Jetzt können Sie Ihre Site-Modelle einfach darauf aufbauen.

Verwenden Sie in den jeweiligen Site-Ansichten @Html.EditorFor(x => x.ControllerActionBlocks), um auf diese Editor-Vorlage zu verweisen. ASP.NET kümmert sich automatisch um den Rest.

SomePage.cshtml (View):

@model SomeModel 

@{ 
    ViewData["Title"] = "SomePage"; 
} 
<h2>@ViewData["Title"].</h2> 
<h3>@ViewData["Message"]</h3> 

@Html.LabelFor(x => x.SomeProperty) 

@Html.EditorFor(x => x.ControllerActionBlocks) 

Beispiel:

SomeModel:

public class SomeModel 
{ 
    public string SomeProperty { get; set; } 
    public IEnumerable<ControllerBlock> ControllerActionBlocks { get; set; } 
} 

In Ihrem Controller, füllen Sie das Modell mit Daten:

public IActionResult SomePage() 
{ 
    var model = new SomeModel() 
    { 
     SomeProperty = "Your application model property.", 
     ControllerActionBlocks = GetControllerBlocks() 
    }; 

    return View(model); 
} 

Zu tun: Definieren Sie Ihre Editor-Vorlage für MD, SM, xs Geräte.

Für weitere Informationen über Spalten auf Bootstrap, Dies ist Definition von GetControllerBlocks()Bootstrap Grid System

besuchen:

public List<ControllerBlock> GetControllerBlocks() 
{ 
    //Set ActionActive herem, if necessary 
    List<ControllerAction> homeActions = new List<ControllerAction>() { 
     new ControllerAction { ActionName = "Index" }, 
     new ControllerAction { ActionName = "DashBoard" }, 
     new ControllerAction { ActionName = "Chart" } 
    }; 
    List<ControllerAction> hrActions = new List<ControllerAction>() { 
     new ControllerAction { ActionName = "Leave" }, 
     new ControllerAction { ActionName = "Loan" }, 
     new ControllerAction { ActionName = "NoticeBoard" } 
     }; 

    List<ControllerAction> payRollActions = new List<ControllerAction>() { 
     new ControllerAction { ActionName = "View" }, 
     new ControllerAction { ActionName = "Edit" }, 
     new ControllerAction { ActionName = "Process" } 
     }; 

    List<ControllerBlock> actionBlocks = new List<ControllerBlock>() 
    { 
     new ControllerBlock(){ControllerName = "Home", ControllerActions = homeActions}, 
     new ControllerBlock(){ControllerName = "HR", ControllerActions = hrActions}, 
     new ControllerBlock(){ControllerName = "PayRoll", ControllerActions = payRollActions} 
    }; 

    return actionBlocks; 
} 
+0

Ihr neuer Code ist in Ordnung, aber eine Anfrage, die bitte fügen Sie Ihren alten Code auch, was Sie vorher gepostet haben, wenn möglich. Danke vielmals. –

+0

@MonojitSarkar Sie können auf "bearbeitete n Stunden" vor, neben meinem Benutzernamen klicken. Scrolle nach unten, du wirst es dort finden. – jAC

+0

vielen dank ......... –

Verwandte Themen