ASP.NET MVC中最简单的动态视图模型3 [英] Simplest Way To Do Dynamic View Models in ASP.NET MVC 3
问题描述
警告:这可能是C#的动态关键字的不当使用,我可能应该使用强类型的视图模型,但...
我试图通过将C#4动态类型传递给我的观点来避免创建一个强类型的视图模型。我的控制器中有这个:
public ActionResult Index()
{
var query =
from fr in db.ForecastRates
join c in db.Codes
on
new {Code = fr.RateCode,CodeType =ForecastRate}
等于
新的{Code = c.CodeValue,CodeType = c.CodeType}
选择新
{
RateCode = fr.RateCode,
RateCodeName = c.CodeName,
年份= fr.Year,
Rate = fr.Rate,
Comment = fr.Comment
};
//创建动态对象列表以形成视图模型
//具有优化速率代码
var forecastRates = new List< dynamic>();
foreach(查询中的var fr)
{
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的剃刀查看引擎):
@inherits System.Web.Mvc.WebViewPage< IEnumerable< dynamic>>
...
< tbody>
@foreach(Model中的var item){
< tr>
< td> @ item.RateCodeName< / td>
< td> @ item.Year< / td>
< td> @ item.Rate< / td>
< td> @ item.Comment< / td>
< / tr>
}
< / tbody>
我不喜欢我如何迭代LINQ结果来形成动态对象列表。 / p>
我想初始化LINQ查询中的每个ExpandoObject,但是似乎不被支持。
我试过投射查询结果为列表,但由于无法将匿名类型转换为动态,因此无法正常工作。
说,不支持。 (我不是说动态视图模型不支持 - 我在说你想做什么不是)
你可以整理LINQ查询,但最终你最好的选择就是创建一个自定义的View Model。认真地说,这将需要大约30秒的时间。
我知道动态是新的,很酷和一切,但你的代码将会更加整洁,更容易维护在这种情况下,如果您只是坚持使用自定义的View Model。
我只会在非常简单的场景中使用动态View Model(大多数时候可能需要)坚持我们一直在做的一切 - 自定义查看模型。
Caveat: This might be an inappropriate use of C#'s dynamic keyword and I probably should be using a strongly-typed view model, but...
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);
}
...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>
I don't like how I iterate through the LINQ result to form the List of dynamic objects.
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)
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.
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屋!