速记同时具有模型和ViewData的项目创造的ViewDataDictionary? [英] Shorthand for creating a ViewDataDictionary with both a model and ViewData items?

查看:477
本文介绍了速记同时具有模型和ViewData的项目创造的ViewDataDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来创建一个的ViewDataDictionary 与模型和附加属性与code一行。我试图让不同时跨多行明确组装的ViewDataDictionary装配两个模型和一些额外的显示配置属性的RenderPartial 调用一个强类型的视图。看起来这将有可能给出的的RenderPartial 超载服用这两种模型对象的ViewDataDictionary ,但它看起来像它只是忽略了的ViewDataDictionary 时,他们都填充。

Is there any way to create a ViewDataDictionary with a model and additional properties with a single line of code. I am trying to make a RenderPartial call to a strongly-typed view while assembling both the model and some extra display configuration properties without explicitly assembling the ViewDataDictionary across multiple lines. It seems like it would be possible given the RenderPartial overload taking both a model object and a ViewDataDictionary but it looks like it simply ignores the ViewDataDictionary whenever they are both populated.

// FAIL: This will result in ViewData being a ViewDataDictionary
// where Model = MyModelObject and there are no other parameters available.
this.Html.RenderPartial("SomePartialView", MyModelObject, new ViewDataDictionary(new { SomeDisplayParameter = true }));

我发现别人用<一个href=\"http://stackoverflow.com/questions/495351/asp-net-mvc-rc1-renderpartial-viewdatadictionary\">same问题,但他们的解决方案是相同的多线的概念,我发现:创建离散的ViewDataDictionary 与模型,添加新的参数(S),并用它在的RenderPartial 电话。

I found someone else with the same problem, but their solution is the same multi-line concept I found: create a discrete ViewDataDictionary with the model, add the new parameter(s) and use it in the RenderPartial call.

var SomeViewData = new ViewDataDictionary(MyModelObject);
SomeViewData.Add("SomeDisplayParameter", true);
this.Html.RenderPartial("SomePartialView", SomeViewData);

我可以随时换的逻辑成返回添加了新的元素重复字典中的 ChainedAdd 方法,但它只是好像我缺少创造的一些方法的ViewDataDictionary 这将帮我这个忙(这是一个比较开销比我所期待的)。

I can always wrap that logic into a ChainedAdd method that returns a duplicate dictionary with the new element added but it just seems like I am missing some way of creating a ViewDataDictionary that would do this for me (and that is a bit more overhead than I was hoping for).

this.Html.RenderPartial("SomePartialView", new ViewDataDictionary(MyModelObject).ChainedAdd("SomeDisplayParameter", true));

public static ViewDataDictionaryExtensions {
    public static ViewDataDictionary ChainedAdd(this ViewDataDictionary source, string key, object value) {
        return source.ChainedAdd(new KeyValuePair<string,object>(key, value));
    }
    public static ViewDataDictionary ChainedAdd(this ViewDataDictionary source, KeyValuePair<string, object> keyAndValue) {
        ViewDataDictionary NewDictionary = new ViewDataDictionary(source);
        NewDictionary.Add(keyAndValue);
        return NewDictionary;
    }
}

同时,试图组装一个的ViewDataDictionary 有一个明确的模式的ModelState 只是会导致编译错误,因为ModelState中是只读的。

As well, trying to assemble a ViewDataDictionary with an explicit Model and ModelState simply causes a compilation error because the ModelState is read-only.

// FAIL: Compilation error
this.Html.RenderPartial("SomePartialView", new ViewDataDictionary { Model = MyModelObject, ModelState = new ViewDataDictionary( new { SomeDisplayParameter = true }});

ANSWER(S):它看起来像克雷格和我最终发现,将完成这项工作两个单独的语法。我在这种情况下肯定有失偏颇,但我喜欢先设置模式,继装饰它的想法。

ANSWER(S): It looks like Craig and I ended up finding two separate syntaxes that will get the job done. I am definitely biased in this case, but I like the idea of setting the model first and "decorating" it afterwards.

new ViewDataDictionary(MyModelObject) { { "SomeDisplayParameter", true }, { "SomeOtherParameter", 3 }, { "SomeThirdParameter", "red" } };

new ViewDataDictionary(new ViewDataDictionary() { {"SomeDisplayParameter", true }})
    { Model = MyModelObject };

当然,我仍然会纺纱我的车轮没有他[最终发现上]答案,所以,圆获取广场。

Of course, I would still be spinning my wheels without his [eventually spot-on] answer, so, circle gets the square.

推荐答案

使用的<一个href=\"http://blogs.msdn.com/howard%5Fdierking/archive/2007/08/13/c-3-0-object-initializers.aspx\">object初始化并集合初始化:

new ViewDataDictionary(new ViewDataDictionary() { {"SomeDisplayParameter", true }})
    {
        Model = MyModelObject
    }

内的ViewDataDictionary获取其集合初始化,那么这个填充使用真实的ViewDataDictionary的构造函数重载这需要,而不是对象的ViewDataDictionary。最后,对象初始化设置模式。

The inner ViewDataDictionary gets its collection initialized, then this populates the "real" ViewDataDictionary using the constructor overload which takes ViewDataDictionary instead of object. Finally, the object initializer sets the model.

然后,只需通过整个事情,而不单独设置MyModelObject:

Then just pass the whole thing without setting MyModelObject separately:

this.Html.RenderPartial("SomePartialView", null, 
    new ViewDataDictionary(new ViewDataDictionary() { {"SomeDisplayParameter", true }})
        { Model = MyModelObject });

这篇关于速记同时具有模型和ViewData的项目创造的ViewDataDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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