EditorFor()和additionalViewData:如何在辅助类添加数据? [英] EditorFor() and additionalViewData: how to add data in helper class?

查看:158
本文介绍了EditorFor()和additionalViewData:如何在辅助类添加数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EditorFor()可以带一个对象additionalViewData 参数典型的方法来填充它是这样的:

EditorFor() can take an object additionalViewData parameter which the typical method to populate is something like:

EditorFor(型号=> model.PropertyName,新{=的myKeymyvalue的})

我怎么能检查additionalViewData的内容,添加或附加到现有值的关键,在等一个自定义的HTML帮助?

How can I inspect the contents of additionalViewData, add or append to existing value for a key, etc in a custom HTML Helper?

我已经试过这些方法:


  • 转换为词典<字符串对象>()添加/追加值:不工作,因为它看起来像EditorFor的MVC实现使用新RouteValueDictionary( additionalViewData)中嵌入一个字典
  • 中的字典
  • 转换成RouteValueDictionary使用新RouteValueDictionary(additionalViewData)但具有相同(或相似)问题如上

  • convert to Dictionary<string, object>() and add/append values: doesn't work as it looks like the implementation of EditorFor in MVC uses new RouteValueDictionary(additionalViewData) which embeds the dictionary within a dictionary
  • convert to RouteValueDictionary using new RouteValueDictionary(additionalViewData) but that has same (or very similar) issue as above

我也开到你这样做是错误的 - 也许我缺少一个简单的方法。请记住什么,我试图做的是写一个HTML帮手是可重复使用的,并增加了通过自定义视图可以用来additionalViewData一些值。一些值依赖于从属性的元数据,所以它不是那么容易的只是用一堆不同的模板。

I'm also open to "you're doing it wrong" -- maybe I'm missing a simpler approach. Keep in mind what I'm trying to do is write an HTML helper that is reusable and adds some values to the additionalViewData to be used by custom views. Some of the values depend on metadata from the property so it is not quite so easy as just use a bunch of different templates.

更新与例如我在做什么的:

Update with example of what I'm doing:

    public static MvcHtmlString myNullableBooleanFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> choice, string templateName, object additionalViewData)
    {            
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(choice, htmlHelper.ViewData);

        /*
    here need to add to additionalViewData some key values among them:
    { propertyName, metadata.PropertyName }

     */

        StringBuilder sb = new StringBuilder();
        sb.AppendLine(htmlHelper.EditorFor(choice, templateName, additionalViewData).ToString());
        MvcHtmlString validation = htmlHelper.ValidationMessageFor(choice);
        if (validation != null)
            sb.AppendLine(validation.ToString());
        return new MvcHtmlString(sb.ToString());
    }

更新与当我匿名对象转换为会发生什么词典&LT;字符串对象&gt;()并传递字典 EditorFor()

我把一个破发点中的Razor视图并检查ViewData的。看来,字典传递到 EditorFor()放在另一个词典&LT内部,字符串,对象&gt;()。在立即窗口,ViewData的是这样的:

I put a break point in the Razor view and examined ViewData. It appears that the dictionary passed into EditorFor() is put inside another Dictionary<string, object>(). In the "Immediate Window", ViewData looks like this:

ViewData.Keys
Count = 4
    [0]: "Comparer"
    [1]: "Count"
    [2]: "Keys"
    [3]: "Values"

请参阅该词典是如何之内具有词典的内容?是的,实际的数据是在内部字典然而勿庸置疑,这是行不通的。

See how the dictionary has the contents of a dictionary within it? Yes, the actual data is in that inner dictionary however unsurprisingly, this doesn't work.

增加赏金。

推荐答案

如果我理解正确的话,您要遍历一个匿名类型的属性。如果是这样:怎样遍历一个匿名对象在C#中的属性

If I understand correctly, you are trying to iterate over properties of an anonymous type. If so: How do I iterate over the properties of an anonymous object in C#?


好吧,这不止于此。这是真正困扰我,因为我爱C#和它不会让我做什么Python做,这也是我喜欢。所以这里有一个解决方案,如果您使用的是C#4,但它的混乱,并为类似的问题我有工作,但也许不完全适合你:

Ok, it's more than that. This is really bothering me now because I love C# and it won't let me do what Python does, which I also love. So here's a solution, if you are using C# 4 but it's messy and will work for a similar problem I have but maybe not exactly for you:

    // Assume you've created a class to hold commonly used additional view data
    // called MyAdditionalViewData. I would create an inheritance hierarchy to
    // contain situation-specific (area-specific, in my case) additionalViewData.
class MyAdditionalViewData
{
    public string Name { get; set; }

    public string Sound { get; set; }
}     

/* In your views */
// Pass an instance of this class in the call to your HTML Helper
EditorFor(model => model.PropertyName, 
    new { adv = new MyAdditionalViewData { Name = "Cow", Sound = "Moo" } }) ;               

    /* In your HTML helper */
    // Here's the C# 4 part that makes this work.
dynamic bovine = new ExpandoObject();

// Copy values
var adv = x.adv;
if (adv.Name != null) bovine.Name = adv.Name;
if (adv.Sound != null) bovine.Sound = adv.Sound;

// Additional stuff
bovine.Food = "Grass";
bovine.Contents = "Burgers";

    // When the MVC framework converts this to a route value dictionary
    // you'll get a proper object, not a dictionary within a dictionary
var dict = new RouteValueDictionary(bovine);

有拥有的获得的是一个更好的办法。

There has got to be a better way.

这篇关于EditorFor()和additionalViewData:如何在辅助类添加数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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