Creggdev
Creggdev

Reputation: 291

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

Upvotes: 0

Views: 5244

Answers (2)

GTInsight
GTInsight

Reputation: 68

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")

Upvotes: 1

Amirhossein Mehrvarzi
Amirhossein Mehrvarzi

Reputation: 18954

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.

Upvotes: 1

Related Questions