Reputation: 1458
I'm trying to find the correct way to do this and I'd appreciate some direction in how to do it. I have a scaffolded entity framework controller with views, and multiple items in my model that refer back to an enum:
public enum JobStatus
{
NotStarted, InProgress, AwaitingReboot, Completed
}
public class Job
{
public int JobID { get; set; }
public string CompanyName { get; set; }
public string EngineerName { get; set; }
public string ServerName { get; set; }
public JobStatus? OverallJobStatus { get; set; }
public JobStatus? WindowsUpdateCompleted { get; set; }
public JobStatus? MicrosoftUpdateCompleted { get; set; }
public JobStatus? ExchangeUpdateCompleted { get; set; }
public JobStatus? FirmwareUpdateCompleted { get; set; }
public JobStatus? SymantecSEPMUpdateCompleted { get; set; }
public JobStatus? SymantecClientsUpdateCompleted { get; set; }
public JobStatus? BackupSoftwareUpdateCompleted { get; set; }
public JobStatus? BackupManualCheckCompleted { get; set; }
public JobStatus? EventViewerCheckCompleted { get; set; }
public JobStatus? DiskDefragCompleted { get; set; }
public JobStatus? RecreateSBSMonitoring { get; set; }
The index.cshtml is referencing this as (here is one for example):
@Html.DisplayFor(modelItem => item.ExchangeUpdateCompleted)
This all works fine, and in the relevant columns per row I get the job status of NotStarted or AwaitingReboot etc.
I have four span classes that represent a bootstrap glyphicon:
<span class="glyphicon glyphicon-ok green"></span>
<span class="glyphicon glyphicon-remove red"></span>
<span class="glyphicon glyphicon-star gold"></span>
<span class="glyphicon glyphicon-off red"></span>
What I want to do, instead of displaying NotStarted or AwaitingReboot etc I want to display the span class instead.
I'd assume this would require a switch/case statement. Where would the proper place be for this in an MVC framework? I'd assume the most efficient way to do this would be a function of some sort where I pass the jobstatus and it outputs the correct span class? Any direction with a code example would be appreciated.
Based on this how would I display this in the actual code
Upvotes: 2
Views: 269
Reputation: 119017
I would do this using a display template.
First create the folder /Views/Shared/DisplayTemplates
and in there add a view called JobStatus.cshtml
(i.e. the name matched the type.) In that view, put the logic for determining the HTML you want to output. For example:
@model QuartelyMaintenance.Models.JobStatus
@{
string icon = "ok";
string colour = "green";
switch (Model)
{
case QuartelyMaintenance.Models.NotStarted:
icon = "star";
colour = "red";
break;
case QuartelyMaintenance.Models.AwaitingReboot :
icon = "off";
colour = "green";
break;
}
}
<span class="glyphicon glyphicon-@icon @colour"></span>
Now you can use it like you do normally and MVC will pick up this template:
@Html.DisplayFor(modelItem => item.ExchangeUpdateCompleted)
Upvotes: 2
Reputation: 11433
I would create a static method in your model class:
public static string GetHtmlForJobStatus(JobStatus status)
{
switch(status)
{
case JobStatus.Completed:
{
return @"<span class="glyphicon glyphicon-ok green"></span>"
}
case JobStatus.Completed:
{
return @"<span class="glyphicon glyphicon-remove red"></span>"
}
// the rest of your cases
}
}
Then, in your view you can just call that for each one:
@Html.Raw(Model.GetHtmlForJobStatus(Model.ExchangeUpdateCompleted))
Upvotes: 2