Hello everyone I’m trying to do change text color in my controller. Yes I did it but I have to send two parameter to my view. So is there any alternative way to change color in controller method ?
Here is my controller:
public action Test ()
{
ViewBag.stackoverflow = "It's gonna be red";
ViewBag.color = "red";
}
And my view:
@{
ViewBag.Title = "Test me";
}
<font color="@ViewBag.color">@ViewBag.stackoverflow</font>
In ASP.NET MVC, Controller and View communicate via Model that you send to view. So there is no way.
But it is good thing. MVC introduces separation of various concerns. One of many advantages is testability.
One more thing. your “color”property on ViewBag is not good idea neither. Try to describe the purpose of highlighted value like “priority” or something else and later in view you can decide the color to use for different levels of priority.