简体   繁体   中英

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?

I implemented the suggested solution using an editor template but my View was only displaying the first property in the embedded IEnumerable . I implemented both solutions from the above post but the one with the EditorFor template is not working for me. My view displays only the ID property.

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.

Define your template in the EditorFor extension method like below:

@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.
  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
  3. Use Simple models inside templates like string , DateTime?
  4. For working with IEnumerable models for template, consider a very important tip available in EditorFor IEnumerable with TemplateName .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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