如何设置继承partialviewresult的类模型 [英] How to set a model with a class that inherits partialviewresult

查看:154
本文介绍了如何设置继承partialviewresult的类模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经基于一个动态的CSS的解决方案,这里所说的:

I've based a dynamic css solution as mentioned here:

动态CSS的ASP.NET MVC?

我注意到继承PartialViewResult只具有示范一个getter,有另一种方式我可以实现这个功能,其中HttpContext.Response.ContentType =文/ CSS和你一样可以用partialview我可以在发送模型?

I notice the inherited PartialViewResult only has a getter for the Model, is there another way I can implement this functionality where HttpContext.Response.ContentType = "text/css" and I can send in a model like you can with a partialview?

推荐答案

只要适应自定义操作结果,这样它需要一个模型:

Simply adapt the custom action result so that it takes a 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);
    }
}

和则:

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

和视图:

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

这篇关于如何设置继承partialviewresult的类模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆