4

I am trying to use the model ListModel as a generic list model. I would like to enter on the page

@Html.DisplayForModel()

However the MVC is not correctly finding the templated file "ListModel.cshtml". It must work differently for generic models. What should I name the templated file in order for it to correctly be located?

public class ListModel<T>
{
public IEnumerable<T> Models { get; set; }
public string NextPage { get; set; }
}

I would expect it to look for Shared/DisplayTemplates/ListModel.ascx but it doesn't. Does anyone know?

Edit:

I did end up solving this by simply removing the generic parameter like so. I do want to know if you can still have a generic file name though.

public class ListModel
{
    public IEnumerable Models {get;set;}
    public string NextPage {get;set;}
}
2
  • I don't understand anything of what you're trying to do, could you explain again ? Commented Dec 25, 2010 at 23:03
  • 1
    could DisplayForModel overload that takes template name help? Commented Dec 26, 2010 at 20:14

3 Answers 3

2

As a workaround, could you decorate the ListModel<T> class with the UIHint attribute to force it to use the template you want?

For example,

[UIHint("ListModel")]
public class ListModel<T>
{
...
Sign up to request clarification or add additional context in comments.

2 Comments

The UIHint attribute cannot be applied to classes like this, only properties and fields.
@Darren, that's a damn shame :) This would have been perfect
2

I don't think this is possible.

Think about it: If you somehow was able to declare the template as generic (say by calling it ListModel`1.ascx or something) how would the MVC runtime handle any specific instances of the model? And how would you display the generic properties/fields in the template?

I haven't been able to find a place where MS specifically states that generic models are disallowed, but I can't see how they would make it work.

This is also supported by the observation that if you try to create a strongly-typed view, then generic classes are filtered out of the drop-down box.

2 Comments

It would be able t decide what to do with the properties through contravariance and the dynamic type.
It could use other DisplayTemplate/EditorTemplate for instances of the generic type just as you could render any type using Object.cshtml template. It'll be really useful if you're able to provide templates for rendering a collection of items e.g. TableListView IconsView MasterDetailsView etc. etc. But I still agree that this isn't possible currently. However it is a totally implementable solution.
0

You can explicitly specify your Template, then MVC will find it as usual.

@Html.DisplayForModel("ListModel")

and similarly

@Html.DisplayFor(m=>m.AProperty,"TemplateName")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.