最简单的方式做动态视图模式在ASP.NET MVC 3 [英] Simplest Way To Do Dynamic View Models in ASP.NET MVC 3

查看:145
本文介绍了最简单的方式做动态视图模式在ASP.NET MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

买者:这可能是一个不恰当的使用C#的动态关键字,我大概应该使用强类型的视图模式,但...

Caveat: This might be an inappropriate use of C#'s dynamic keyword and I probably should be using a strongly-typed view model, but...

我试图避免创建通过将C#4动态类型我认为一个强类型的视图模式。我有这个在我的控制器:

I'm trying to avoid creating a strongly-typed view model by passing a C# 4 dynamic type to my view. I have this in my controller:

    public ActionResult Index()
    {
        var query =
            from fr in db.ForecastRates
            join c in db.Codes
            on 
                new { Code = fr.RateCode, CodeType = "ForecastRate" }
            equals 
                new { Code = c.CodeValue, CodeType = c.CodeType }
            select new
            {
                RateCode = fr.RateCode,
                RateCodeName = c.CodeName,
                Year = fr.Year,
                Rate = fr.Rate,
                Comment = fr.Comment
            };

        // Create a list of dynamic objects to form the view model
        // that has prettified rate code
        var forecastRates = new List<dynamic>();

        foreach (var fr in query)
        {
            dynamic f = new ExpandoObject();

            f.RateCode = fr.RateCode;
            f.RateCodeName = fr.RateCodeName;
            f.Year = fr.Year;
            f.Rate = fr.Rate;
            f.Comment = fr.Comment;

            forecastRates.Add(f);
        }

        return View(forecastRates);
    }

...这在我看来(我使用MVC 3的剃刀视图引擎):

...and this in my view (I'm using MVC 3's Razor view engine):

        @inherits System.Web.Mvc.WebViewPage<IEnumerable<dynamic>>

        ...

        <tbody>
            @foreach (var item in Model) {
            <tr>
                <td>@item.RateCodeName</td>
                <td>@item.Year</td>                            
                <td>@item.Rate</td>
                <td>@item.Comment</td>
            </tr>
            }
        </tbody>

我不喜欢我通过LINQ结果如何循环,形成动态的对象列表。

I don't like how I iterate through the LINQ result to form the List of dynamic objects.

我想初始化每个ExpandoObject LINQ查询里面,但是的似乎并不支持

I'd like to initialize each ExpandoObject inside the LINQ query, but that doesn't seem to be supported.

我试过铸造的查询结果列表,但没有工作,因为你不能匿名类型转换为动态的。

I tried casting the the query result as List, but that didn't work because you can't convert anonymous type to dynamic.

推荐答案

就像你说的,它不支持。 (我不是说动态视图模型不支持 - 我说你想要做的是不是)

Like you said, it's not supported. (I'm not saying dynamic View Models aren't supported - I'm saying what you're trying to do is not)

您也许可以neaten了LINQ查询,但最终你最好的选择是简单地创建一个自定义视图模型。严重的是,它会带你约30秒,以做到这一点。

You could probably neaten up the LINQ query, but in the end your best bet would be to simply create a custom View Model. Seriously, it will take you about 30 seconds to do that.

我知道是动态的新酷和一切,但你的code会大量整洁,维护更简单,如果你只是在这种情况下,自定义视图模型坚持下去。

I know dynamic is new and cool and everything, but your code will be a lot neater and easier to maintain if you just stick with a custom View Model in this case.

我只会用很简单的场景动态视图模型去 - 你最可能要坚持我们一直有做什么的时候 - 自定义视图模型

I would only go with a dynamic View Model in the very simple scenarios - most of the time you probably want to stick with what we've been doing all along - custom View Models.

这篇关于最简单的方式做动态视图模式在ASP.NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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