这是我的.css
文件,
@model StyleProfile
body {
color: @Model.color;
}
我将其包含在我的布局中,
<link href="@Url.Action("CssDynamic")" rel="stylesheet" type="text/css" />
我的 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);
}
}
它工作正常。但是我需要将模型发送到 View ,当我在 ActionMethod
"CssDynamic"
return new CssViewResult(model);
错误说
"this does not contain a constructor that takes 1 arguments.
如何修改 CssViewResult
类来解决这个问题?
最佳答案
您错过了 CssViewResult 类的参数化构造函数。在 CssViewResult 类中的 ExecuteResult 方法之前添加以下内容。
public CssViewResult(object model)
{
ViewData = new ViewDataDictionary(model);
}
另请参阅此 stackoverflow 帖子。
How to set a model with a class that inherits partialviewresult
关于c# - 在 asp.net mvc 中使用 Razor 的 Css View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29919754/