简体   繁体   English

ASP.NET MVC编辑器模板不起作用

[英]ASP.NET MVC Editor Template Not Working

After having problems getting a Razor View to render a ViewModel with an embedded IEnumerable I came across the following post: View Model IEnumerable<> property is coming back null (not binding) from post method? 在获取Razor视图以呈现带有嵌入式IEnumerable的ViewModel遇到问题之后,我遇到了以下帖子: View Model IEnumerable <>属性从post方法返回空值(不绑定)吗?

I implemented the suggested solution using an editor template but my View was only displaying the first property in the embedded IEnumerable . 我使用编辑器模板实现了建议的解决方案,但我的视图仅在嵌入式IEnumerable显示第一个属性。 I implemented both solutions from the above post but the one with the EditorFor template is not working for me. 我实现了以上文章中的两种解决方案,但是带有EditorFor模板的解决方案对我不起作用。 My view displays only the ID property. 我的视图仅显示ID属性。

Does anyone have any ideas why the soution with the editor template may not be working for my setup? 有谁知道为什么带有编辑器模板的部分可能不适用于我的设置? I have implemented it exactly as suggested. 我已经完全按照建议实施了它。

If your view is only displaying the first item then your template is not being picked up in the EditorTemplates folder. 如果您的视图仅显示第一项,则不会在EditorTemplates文件夹中拾取您的模板。

Define your template in the EditorFor extension method like below: 在EditorFor扩展方法中定义模板,如下所示:

@Html.EditorFor(model => model, "NameofEditorTemplate")

For using Editor Template consider the following tips : 要使用编辑器模板,请考虑以下提示

  1. You have to place templates inside EditorTemplates folder in Views folder. 您必须将模板放在Views文件夹的EditorTemplates文件夹内。
  2. You should use EditorFor HTML helper to call appropriate template (when you have specific model to view, It will find the desired template automatically, otherwise you have to hint the engine, whether by an argument in EditorFor or using [UIHint] attribute in the model. Look at Extending Editor Templates for ASP.NET MVC 您应该使用EditorFor HTML助手来调用适当的模板(当您要查看特定的模型时,它将自动找到所需的模板,否则,您必须通过EditorFor的参数或使用模型中的[UIHint]属性来提示引擎。查看扩展ASP.NET MVC的编辑器模板
  3. Use Simple models inside templates like string , DateTime? 在模板内部使用简单模型,例如stringDateTime?
  4. For working with IEnumerable models for template, consider a very important tip available in EditorFor IEnumerable with TemplateName . 为了使用IEnumerable模型的模板,请考虑一个非常重要的技巧,该技巧在EditorFor IEnumerable with TemplateName中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM