Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change css dynamically from controller

Tags:

c#

asp.net-mvc

I'm new with ASP.NET MVC, at the cshtml I have some style:

 .circle {
     border: 2px solid red;
     background-color: #FFFFFF;
     height: 100px;
     border-radius:50%;
     width: 100px;
 }

<div class="circle"></div>

And I want to change the circle position from the controller, I can I achieve that? In the controller I need something like:

circle.MarginLeft = 120; 
like image 747
Devy Avatar asked Apr 22 '26 05:04

Devy


1 Answers

You can use your model or ViewBag.

In controller pass your data into an object like circle.MarginLeft = 12 or use ViewBag['marginLeft'] = 12

and in your .cshtml file set

<div class="circle" style="margin-left:@ViewBag.marginLeft"></div>

or

<div class="circle" style="margin-left:@Model.marginLeft"></div>
like image 176
habib Avatar answered Apr 23 '26 19:04

habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!