proper razor syntax for switch statement inside foreach
asp.net-mvc, razor
Solution
You can do as Justin suggests, something in the way of this:
@foreach (var item in Model) {
String s = item.RegistrationStatus.ToString();
// Make sure this mirrors values in RegistrationStatus enum!
switch (s)
{
case "New":
@:<tr class='info'>
break;
case "Arrived":
@:<tr class='success'>
break;
default:
@:<tr>
break;
}
......
}
But, if you're running MVC4 with Razor V2, you could as easily use a helper method (or regular method) instead:
public static class MyHelperExtensions
{
public static string GetCssClass(this HtmlHelper helper, RegistrationStatus status)
{
// Make sure this mirrors values in RegistrationStatus enum!
switch (status)
{
case RegistrationStatus.New:
return "info";
case RegistrationStatus.Arrived:
return "success";
default:
return null; // Return null so that the attribute won't render.
}
}
}
And then use it like so:
@foreach (var item in Model)
{
<tr class='@Html.GetCssClass(item.RegistrationStatus)'>
.....
}
This is a bit more readable and easier to maintain. If the GetCssClass() method returns `null` then Razor V2 won't even render the attribute (in this case `class=`).
Problem
I am having a heck of a time trying to find the proper syntax for creating a switch statement inside a foreach loop on my mvc view page. Here is the latest attempt (after many others) I have so far but the the Razor engine won't accept it. Here the error is right at the `@foreach` and indicates it is missing a closing `}` ``` @foreach (var item in Model) { String s = item.RegistrationStatus.ToString(); // Make sure this mirrors values in RegistrationStatus enum! switch (s) { case "New": <tr class='info'> break; case "Arrived": <tr class='success'> break; default: <tr> } ...... } ```