How to install a model with a class that inherits partialviewresult

I based a dynamic css solution as stated here:

Dynamic CSS for ASP.NET MVC?

I noticed that the inherited PartialViewResult only has a getter for the model, is there any other way to implement this function where HttpContext.Response.ContentType = "text / css", and I can send the model as you can with partial view

+3
source share
1 answer

Just adapt the result of the user action so that it takes the model:

public class CssViewResult : PartialViewResult
{
    public CssViewResult(object model)
    {
        ViewData = new ViewDataDictionary(model);
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "text/css";
        base.ExecuteResult(context);
    }
}

and then:

public ActionResult Css()
{
    MyViewModel model = ...
    return new CssViewResult(model);
 }

and in view:

@model MyViewModel
@{
    var color = "White";
    if (Model.Hour > 18 || Model.Hour < 8)
    {
        color = "Black";
    }
}
.foo {
    color: @color;
}
+9
source

All Articles