0

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 IDproperty.

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.

Community
  • 1
  • 1
Creggdev
  • 261
  • 4
  • 9
  • 3
    Please actually include some of the code from your setup. Otherwise, we are only making assumptions. – Josh Mein Jul 11 '14 at 16:44
  • 1
    Nope, no idea... because you didn't include any code. – Erik Funkenbusch Jul 11 '14 at 20:12
  • all the code is in the original post......not point cutting and pasting it. As I said I implemented both suggested solutions. The one with the for loop worked but the Editor template one only displayed the first property of the enumerable property. Anyway, I will move on with the for loop and wonder why the Editor Template did not work. – Creggdev Jul 14 '14 at 08:48

2 Answers2

1

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")
podiluska
  • 50,950
  • 7
  • 98
  • 104
GTInsight
  • 68
  • 6
1

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.
Community
  • 1
  • 1
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70