4

This is my view as a .css file,

@model StyleProfile

body {

color: @Model.color;
}

and I included this to my layout,

<link href="@Url.Action("CssDynamic")" rel="stylesheet" type="text/css" />

I have this in my controller

 public class HomeController : Controller
{
    private OnepageCMSEntities db = new OnepageCMSEntities();
    public ActionResult CssDynamic()
    {
        var model = db.StyleProfiles.FirstOrDefault();
        return new CssViewResult();
    }
}

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

It works fine. But I need to send a model to the view and the problem appears when I send a model object in the ActionMethod "CssDynamic"

 return new CssViewResult(model);

The error says

"this does not contain a constructor that takes 1 arguments.

How can I modify CssViewResult class to solve this problem?

7
  • 1
    Why are you sending a model to a view and trying to send it as a CSS file? Can you give some background on what you are trying to achieve? There may be a better way. Commented Apr 28, 2015 at 12:41
  • You are missing a contructor in your CssViewResult Class. Explain what exactly are you trying to achieve. Commented Apr 28, 2015 at 12:43
  • @HarveySpecter I would imagine OP is wanting to use the default constructor from PartialViewResult Commented Apr 28, 2015 at 12:44
  • Possibly, but clearly wont work since he needs a contructor of CssViewResult. Commented Apr 28, 2015 at 12:44
  • i want to use this to dynamicaly change my css file . i need to send this model object to ~/home/CssDynamic (Strongly type view ,that is my css file). Commented Apr 28, 2015 at 12:46

2 Answers 2

3

You missed the parameterized constructor for CssViewResult class. Add the following before ExecuteResult method in CssViewResult class.

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

Also refer this stackoverflow post for the same.

How to set a model with a class that inherits partialviewresult

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

Comments

1

You should to create the constructor with appropriate argument like this:

    public class CssViewResult : PartialViewResult
    {
        private readonly object model;

        public CssViewResult(object model)
        {
            this.model = model;
        }

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

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.