Radu Stanescu
Radu Stanescu

Reputation: 37

How to change css style from server side MVC.NET

I know I can do something like this in ASP.NET

<body id="body" runat="server">

And in .aspx.cs

this.body.Style["Background-Color"] = "blue";

But how can I do the same thing in MVC.NET? How do I write my controller and my view?

Upvotes: 0

Views: 4230

Answers (1)

kamil-mrzyglod
kamil-mrzyglod

Reputation: 4998

There is no way to change CSS from server side in MVC because it works completely differently comparing to the WebForms.

If you want to pass some value to the view from the controller(and use it e.g. as a CSS property), you can do following:

public ActionResult SomeAction()
{
    ViewBag.BackgroundColor = "blue";
    return View();
}

then in your view:

<body id="body" style="background-color: @ViewBag.BackgroundColor">

Or just pass this value as a model property. BTW: in my opinion you shouldn't pass values like background color from controller to the view since a view shouldn't depend directly on some server side properties.

Upvotes: 1

Related Questions