0

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?

2
  • You can't do this in MVC. Commented Jul 29, 2015 at 7:33
  • Ok, so how can I change the css style from server side? Any other method? Commented Jul 29, 2015 at 7:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.