简体   繁体   English

Razor MVC 中的动态 CSS

[英]Dynamic CSS in Razor MVC

I am trying to get different css on input.我试图在输入上获得不同的 css。 using Custom razor view Engine.使用自定义剃刀视图引擎。 and it works if I just take data from Database from 1 collction.如果我只是从 1 个集合中的数据库中获取数据,它就可以工作。 but I want to alternate the CSS for lets say different users.但我想为不同的用户交替使用 CSS。

I was using custom Foo.CSCSS as view where my razor CSS was held.我使用自定义 Foo.CSCSS 作为我的 razor CSS 所在的视图。 And MyViewModel gets data from DB. MyViewModel 从 DB 获取数据。

Like this.像这样。

public MongoDatabase Mongo;
    [HttpPost]
            public ActionResult Foo(string Input)
            {
                var collection = Mongo.GetCollection<MyViewModel>(Input);
                var model = collection.FindAll().ToList<MyViewModel>().FirstOrDefault();
    return View(model);

    @using (Html.BeginForm("Foo", "Styles", FormMethod.Post))
    {
        <fieldset>
            <label for="sub">Get you CSS.</label>
            <br />
            <input id="txtEmail" type="text" name="Input" placeholder="Type in an CSS collection" required>
            <input type="submit" name="submit" value="Press">
        </fieldset>

Returns me a CSS as view instead of just inserting it as CSS.将 CSS 作为视图返回给我,而不是将其作为 CSS 插入。

@model CustomViewEngine.Models.MyViewModel

body {
    background-color: red;
}

div.col-md-4 {
    background-color: @Model.BGColor;
    text-decoration: @Model.TextDec;
    border-style: @Model.BRDStyle;
    border-width: @Model.BRDWidth;
}
div.yo {
background-color: blue;
}

Is there a fix for that?有解决办法吗?

public ActionResult Foo()
        {
            var collection = Mongo.GetCollection<MyViewModel>("CollectionName");
            var model = collection.FindAll().ToList<MyViewModel>().FirstOrDefault();
return View(model);

Like this it works and gets data from DB.像这样它可以工作并从数据库获取数据。 And uses it as CSS.并将其用作 CSS。 I probably failed to explain the CSS is not exactly CSS it's a custom extension Foo.CSCSS, I parse and feed as View.我可能没有解释 CSS 不完全是 CSS,它是一个自定义扩展 Foo.CSCSS,我解析并作为视图提供。

I took This as Example Dynamic Stylesheets Using Razor我将此作为使用 Razor 的示例动态样式表

@{
  var thiscss="by default class name";
  if(@model.css=="css1"){
    thiscss = "css1";
  }
  if(@model.css == "css2")
  {
    thiscss ="css2";
  }
}
<div class="@thiscss">
   Help
</div>

I think you need to wrap your dynamic css section inside <style> attribute.我认为您需要将动态 css 部分包装在<style>属性中。 From my end, I tried something like below and it worked:从我的角度来看,我尝试了类似下面的方法并且它起作用了:

<style>
    div.col-md-4 {
       background-color: @Model.BGColor;
       text-decoration: @Model.TextDec;
       border-style: @Model.BRDStyle;
       border-width: @Model.BRDWidth;
     }
</style>

Please comment if issue is still not fixed.如果问题仍未解决,请发表评论。

In MVC you can make your sections render based on condition, code and be mixed with html eaisly, so just move your styles in the css files and render the files based on conditions.在 MVC 中,您可以根据条件、代码渲染您的部分并轻松与 html 混合,因此只需在 css 文件中移动您的样式并根据条件渲染文件。

if(some condition is true)
{
render a style here
}
else if(other condition)
{
render other style
}
else
{
default style
}

you can also put this in your layout page, these condition can be anything, like you said user type checks ad all.你也可以把它放在你的布局页面中,这些条件可以是任何东西,就像你说的用户类型检查广告一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM