MVC3 Html.Editor定位字典未绑定到客户端 [英] MVC3 Html.Editor targeting dictionary is not binding down to the client

查看:67
本文介绍了MVC3 Html.Editor定位字典未绑定到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型与

EditViewModel
    SomeUserType User
        Dictionary<string, string> Claims

在我看来,我正在尝试按以下方式进行绑定

In my view I'm trying to bind this as follows

@{
    int i = 0;
    foreach (var key in Model.User.Claims.Keys)
    {                                                                       
            <div class="editor">
                <input type="hidden" value="@i" name="User.Claims.Index">
                <div class="editor-label">
                    @Html.Label(string.Format("User.Claims[{0}].Key", i))
                </div>
                <div class="editor-field">
                    @Html.Editor(string.Format("User.Claims[{0}].Key", i))
                </div>
                <div class="editor-label">
                    @Html.Label(string.Format("User.Claims[{0}].Value", i))
                </div>
                <div class="editor-field">
                    @Html.Editor(string.Format("User.Claims[{0}].Value", i))
                </div>
            </div>    
        i++;
    }
}

如我所愿,这将在编辑屏幕上呈现一系列键值对"文本框.但是,即使在Claims词典中有KeyValuePairs,文本框也始终是空的.

Which renders out the series of Key Value pair text boxes on the edit screen as I would expect. However the text boxes are always empty even though there are KeyValuePairs in the Claims dictionary.

如果我为KVP文本框填写值,然后将表单发回到我的控制器,我会正确地将值绑定到我的User.Claims词典中.

If I fill in values for the KVP text boxes and post the form back to my controller I do get the values correctly model bound back into my User.Claims Dictionary.

我缺少什么来阻止绑定在视图加载时在其中具有正确的值?

What am I missing that is preventing the binding from having the correct values in it as the view loads?

其他信息,基本上,我试图弄清楚这对于MVC本质上是如何工作的.我直接从核心MVC(2)对象模板创建了上面的视图,该对象模板

Additional information, basically I'm trying to figure out how exactly this works for MVC inherently. I created my view above directly from the core MVC (2) object template that Brad Wilson discusses here. I tried to find the exact code for this in Razor but I couldn't find it at all anywhere... digressing... Converting this to Razor I ended up with

@if (ViewData.TemplateInfo.TemplateDepth > 4) 
{
    @ViewData.ModelMetadata.SimpleDisplayText
}
else
{
    <div class="editor">    
        @foreach (var prop in ViewData.ModelMetadata.Properties
            .Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm)))
        {                        

            if (prop.HideSurroundingHtml)
            {
            @Html.Editor(prop.PropertyName)
            }
            else
            {
                if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName)
                                                            .ToHtmlString()))
                {
                    <div class="editor-label"> @Html.Label(prop.PropertyName) 
                    </div>
                }

                <div class="editor-field">
                    @Html.Editor(prop.PropertyName)
                    @Html.ValidationMessage(prop.PropertyName)
                </div>            
            }
        }
    </div>    
}

主要区别是对@if (ViewData.TemplateInfo.TemplateDepth > 4)的编辑,这将使MVC深入到可以到达我的字典的深度,然后可以正确绑定所有内容.但是,在进一步调试之后,我看到它绑定了Key属性时,实际上只看到@Html.Editor("Key")这是如何工作的?

The main difference is the edit to the @if (ViewData.TemplateInfo.TemplateDepth > 4) which will allow MVC to dive deep enough that it reaches my dictionary it can then bind everything correctly. However upon further interrogation with debugging I see when it binds the Key property it actually sees just @Html.Editor("Key") how does this work?

显然,编辑器必须具有ViewData.ModelMetadata.Properties的某种知识,但是它确定ViewData.TemplateInfo.Visited()的结果是它能够正确放置正确的内容,就在这一点上,我丢失了@ Html.Editor("Key")在此foreach循环中以某种方式在幕后发生的一切.

Clearly Editor must have some kind of knowledge of ViewData.ModelMetadata.Properties and however it determines ViewData.TemplateInfo.Visited()'s result that it's able to correctly place the right stuff, just at this point I'm at a loss of what's happening behind the scenes that somehow inside of this foreach loop that @Html.Editor("Key") does ANYTHING.

推荐答案

@ Html.Editor(字符串名称)将为 ViewData 中的字段名称生成编辑器.如果ViewData不包含您的对象,则必须使用另一个重载:@ Html.Editor(字符串名称,对象viewData).

@Html.Editor(string name) will generate editor for field name in ViewData. If ViewData not contains your object, you must used another overload: @Html.Editor(string name, object viewData).

在您的情况下,您可以将@ Html.Label(string.Format("User.Claims [{0}].Key",i))替换为@ Html.Label(string.Format("User.Claims [ {0}].Key",i),User.Claims [i] .Value)

In your case, you can replace @Html.Label(string.Format("User.Claims[{0}].Key", i)) with @Html.Label(string.Format("User.Claims[{0}].Key", i),User.Claims[i].Value)

这篇关于MVC3 Html.Editor定位字典未绑定到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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