MVC 4 Html.EditorFor不工作 [英] MVC 4 Html.EditorFor is not working

查看:172
本文介绍了MVC 4 Html.EditorFor不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用反射,遍历我对象的所有属性,以显示编辑页面,并为每一财产权编辑控件。
我的观点看起来不够好,做到这一点,我运行的应用程序和编辑控件显示为预期,但,说我想编辑使用id = 4的对象,通常我应该有在页面上此目的,时间,编辑控件针对每个属性,问题是,我得到同样的对象不止一次,对象被重复几次的页面上具有相同的控制和性能。我失去了一些东西在这里?

  ...
@using(Html.BeginForm())
{
    @ Html.ValidationSummary(真)
    <&字段集GT;
        <传奇>测试与LT; /传说>
        <表>
        @ {
            变种道具= Model.GetType()的GetProperties();}
            @foreach(道具VAR道具)
            {
                &所述; TR>
                    < TD>< D​​IV> @ Html.EditorFor(型号=>产品型号,prop.GetValue(型号,NULL))< / DIV>< / TD>
                < / TR>
            }
        < /表>
    < /字段集>
}
...


解决方案

在EditorFor助手的第二个参数根本不去做你认为它。你似乎在试图为它提供一个值,预计该值将在生成的文本框中显示但这并不是什么这个说法是。这种说法被称为 additionalViewData 键,顾名思义可以让你自定义的附加视图数据传递给编辑模板。但是,如果你没有,做一些有用的东西这个观点的数据自定义编辑器模板,你不能指望太多会发生。

我建议你阅读的 下面的博客文章 更好地在ASP.NET MVC模板熟悉。

但真正回答你的问题,你似乎在试图写入,将呈现每个对象的属性的自定义模板。你可以写一个自定义编辑模板,会做这项工作。布拉德·威尔逊表示在这个概念<一href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html\"相对=nofollow> 下面的帖子 (看看部分的浅潜水与深潜的朝的结束文章)。

I want to display an Edit page using reflection, loop through all properties of my object and create the right edit control for each property. My view looks good enough to do this, I run the app and the controls for editing are shown as expected, BUT, say I want to edit object with id=4, normally I should have on the page this object one time with edit controls for each property, problem is that I get the same object more than once, the object is repeated several times on the page with the same controls and properties. Am I missing something here?

...
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Test</legend>
        <table>
        @{
            var props = Model.GetType().GetProperties();}
            @foreach (var prop in props)
            {
                <tr>
                    <td><div>@Html.EditorFor(model => Model, prop.GetValue(Model, null))</div></td>
                </tr>                                
            }
        </table>
    </fieldset>
}
...

解决方案

The second argument of the EditorFor helper doesn't do at all what you think it does. You seem to be attempting to provide it with a value and expect that this value will be shown in the generated textbox but that's not what this argument is for. This argument is called additionalViewData and as its name suggests allows you to pass custom additional view data to the editor template. But if you don't have a custom editor template that does something useful with this view data you cannot expect that much will happen.

I would recommend you reading the following blog post to better familiarize with the templates in ASP.NET MVC.

But to actually answer your question, you seem to be attempting to write a custom template that will render each of the properties of your object. You could write a custom editor template that will do this job. Brad Wilson illustrated this concept in the following post (take a look at the section Shallow Dive vs. Deep Dive towards the end of the article).

这篇关于MVC 4 Html.EditorFor不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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