为什么不使用EditorForModel()显示集合(Why is collection not being displayed using EditorForModel())
我想使用EditorForModel()显示一个集合。
根据布拉德威尔逊的博客,这应该是一个没有脑子的人。 但是,它对我不起作用。
控制者:
public ActionResult Index() { var contact = new Contact() { FirstName = "Greg", LastName = "Gum", Age = 27 }; contact.PhoneNumbers = new List<string>(); contact.PhoneNumbers.Add("111"); contact.PhoneNumbers.Add("222"); return View(contact); }
班上:
public class Contact { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public List<String> PhoneNumbers { get; set; } }
风景:
@model WebApplication33.Models.Contact @Html.DisplayForModel()
这显示其他属性就好了,但不显示集合。 如何显示该集合? 我已经尝试并试图让它工作,因为我读过的所有内容似乎都说这应该像上面那样工作。
I would like a collection to be displayed using EditorForModel().
According to this Blog from Brad Wilson this should be a no brainer. However, it just does not work for me.
The Controller:
public ActionResult Index() { var contact = new Contact() { FirstName = "Greg", LastName = "Gum", Age = 27 }; contact.PhoneNumbers = new List<string>(); contact.PhoneNumbers.Add("111"); contact.PhoneNumbers.Add("222"); return View(contact); }
The Class:
public class Contact { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public List<String> PhoneNumbers { get; set; } }
The View:
@model WebApplication33.Models.Contact @Html.DisplayForModel()
This displays the other properties just fine, but not the collection. How do I get the collection to display? I have tried and tried to get this to work, as all the things I have read seem to say this should work just as the above.
原文:https://stackoverflow.com/questions/22512630
最满意答案
我可以验证您的代码在VS 2012上运行正常。您还应该验证其他一些内容。
- 确保解决方案中的所有相关项目都已设置为构建。
- 确保如果基础项目包含某些引用(如System.Web.Extensions),则依赖项目也必须包含相同的引用。
上面的第二点会导致一些非常模糊的错误,而不是明确错误所在。
I have coincidentally stumbled upon why the one enum exists in the namespace while the other one does not.
It looks like the enums declared in the Web Project will not automatically be made available in the Silverlight project. Only if the enum is used somewhere in the Web Project will it actually be included in the namespace and thus be available in the Silverlight project. I'm not sure if this is as per design or is actually a bug in Silverlight.
I was using the "AccountStatus" enum as a variable somewhere inside my Web Project while I wasn't using the "Gender" enum. That is why the one was available and the other not.
What I did to solve the problem was simply to declare a variable of type "Gender" inside one of the functions in my DomainContext class even though I do nothing with it further than just declaring it (I don't think it HAS to be in the DomainContext class - I expect that it will work as long as you declare such a variable in any class in your Web Project.)
(For reference, the way I declared it was to define a function that simply expected the "Gender" as a variable):
public void DoNothingWithGender(Gender myGender) { /* Do nothing */ }
Once I declared this, the Gender enum was available in my Silverlight project. If I DON'T declare such a variable in the Web Project, the enum is not made available to the Silverlight project. Weird behavior, indeed, but this is what solved my problem.
相关问答
更多-
这听起来像我前一段时间提交的错误: https : //bugzilla.xamarin.com/show_bug.cgi?id = 42488 如果是这样,解决方法是引用绑定项目的.dll而不是绑定项目本身。 IOW,构建绑定项目,然后从X.iOS项目中添加对该.dll的引用。 This sounds like a bug I filed a while ago: https://bugzilla.xamarin.com/show_bug.cgi?id=42448 If so, the workaroun ...
-
如果您需要帮助找出命名空间的“结构”,请在Visual Studio中使用对象浏览器或使用Reflector查看其中的内容 System.Windows.Forms.DataVisualization.Charting.Utilities 命名空间。 这可以帮助您了解您可以在每个内容中访问的内容。 If you need help figuring out the 'structure' of the namespaces, use the Object Browser from within Visual ...
-
在xmlns:x的末尾有一个空格char太多了: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml " <---删除xaml之后的最后一个字符 所以它变成: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" There is a space char too much in the end of the xmlns:x: xmlns:x="http://schemas.microsoft.c ...
-
如果其他人有兴趣,我已经发现了这个问题的答案。 在我的主应用程序中,我以.NET Framework 4 Client Profile为目标,但在其中一个引用的程序集中,我将目标定位到完整的.net框架。 虽然intellisense和Visual Studio本身不报告问题,但编译器会抛出错误。 不是与目标框架有关的错误,只是与我的命名空间相关的“类型或命名空间不存在”错误,而不是框架的客户端配置文件版本中缺少的错误。 埃德 I have discovered the answer to this pro ...
-
我可以验证您的代码在VS 2012上运行正常。您还应该验证其他一些内容。 确保解决方案中的所有相关项目都已设置为构建。 确保如果基础项目包含某些引用(如System.Web.Extensions),则依赖项目也必须包含相同的引用。 上面的第二点会导致一些非常模糊的错误,而不是明确错误所在。 I have coincidentally stumbled upon why the one enum exists in the namespace while the other one does not. It ...
-
您可以像这样“root”命名空间: using global::System.ComponentModel; (然后在代码中删除完全限定的引用。) 或者,如果您确实想要使用完全限定的命名空间: private global::System.ComponentModel.IContainer components = null; private global::System.ServiceProcess.ServiceInstaller serviceInstaller; 这看起来与其他依赖问题无关。 我 ...
-
在使用与其命名空间同名的类之前,我遇到过类似的问题(尽管不完全相同)。 奇怪的是,它似乎在一些开发人员的PC上编译好,但在其他开发人员上没有。 最后,我们确保没有名称空间包含同名的类。 namespace Project.Logging { public static class Logging // this is what caused the probems for me { } } I have run into similar (though not exactly t ...
-
你的构造函数有点偏,你试图这样做this.IStatementRepository ,但变量是this._repo 。 这些错误是因为Visual Studio告诉你在this (你的控制器)里面没有一个名为IStatementRepository的变量。 尝试这个: using StatementsApplication.Models; namespace StatementsApplication.Controllers { public class StatementController : ...
-
在解决方案资源管理器中为System.Web.Mvc参考设置Copy Local = True (通过选择引用并单击F4)。 Set Copy Local = True for System.Web.Mvc Reference in Solution Explorer (by selecting the reference and clicking F4).
-
回过头来完成我对TFS的工作后,我设法解决了我所做的创建了这个错误的事情:我在项目中添加了一个名为“System”的文件夹,并在其中添加了一个类文件。 这是一个容易重现的问题:创建一个项目,向它添加一个名为system的文件夹(此时它仍然会编译),然后在其中创建一个.cs文件,这就是所有有趣的错误都会发生的时候。 问题源于“System”文件夹的名称,该文件夹导致在其中创建的任何文件位于名称空间“.System”下。 我可以理解为什么有一个名为“System”的文件夹/命名空间现在会引起问题,但我认为如果V ...