Conditional Formatting in a Razor Index View?

Razor requires that tags directly inside code blocks be balanced and well-formed. Therefore, all of the code after the first opening tr tag is actually parsed as markup, so that the final } just closes the if .

Up vote 0 down vote favorite share g+ share fb share tw.

I'm trying to conditionally add a CSS background-color to a set of table rows, based on how close the item's expiry date is. Thirty days or less should be red, 90 - 31 days amber and the rest green. (I'm putting the red in first, once this is working I'll go back and do the amber/green rows).

@foreach (var item in Model) { int daysLeft = (item. ExpiryDate - DateTime. Today).

Days; if (daysLeft } else { } @Html. DisplayFor(modelItem => item. SupplierName) @Html.

DisplayFor(modelItem => item. ExpiryDate) @Html. DisplayFor(modelItem => item.

InceptionDate) @Html. DisplayFor(modelItem => item. Value) @Html.

ActionLink("Edit", "Edit", new { id = item. Id }) | @Html. ActionLink("Details", "Details", new { id = item.

Id }) | @Html. ActionLink("Delete", "Delete", new { id = item. Id }) } When I run this page, I get a YSOD saying the @foreach block is missing its closing }, but as far as I can see they are matched so I'm assuming the actual problem is something else.

Asp. Net-mvc-3 razor link|improve this question asked Mar 14 at 17:26PhilPursglove6,3392822 92% accept rate.

Razor requires that tags directly inside code blocks be balanced and well-formed. Therefore, all of the code after the first opening tag is actually parsed as markup, so that the final } just closes the if. To fix that, you can force Razor to ignore the tag by prefixing the line with @:.

Alternatively, you can get rid of the if entirely and write string style = daysLeft ...

True_expression* : *false_expression*; msdn.microsoft.com/en-us/library/ty67wk2... @foreach (var item in Model) { int daysLeft = (item. ExpiryDate - DateTime. Today).

Days; ..... }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions