Reputation: 7694
I am trying to create an Editortemplate for a List<Package>
. I am fully qualifying the type in my Editortemplate view as such:
@model List<JYP.Business.ViewModels.Package>
When I try to reference Model within my Editortemplate view, I am getting a null reference which leads me to believe that the model isn't getting associated properly. I had to use a UIHint
in my ViewModel in order to have it use the Editortemplate at all. My ViewModel contains a List<Package> Packages
which is what I am trying to have my custom Editortemplate pick up. What am I doing wrong?
Upvotes: 1
Views: 314
Reputation: 1039508
You could rely on conventions:
public class MyViewModel
{
public List<Package> Packages { get; set; }
}
and then in your main view:
@model MyViewModel
@Html.EditorFor(x => x.Packages)
and then you could define an editor template which will be automatically rendered for each element of the Packages collection:
@model JYP.Business.ViewModels.Package
...
By convention this editor template should be placed in the ~/Views/SomeController/EditorTemplates/Package.cshtml
or ~/Views/Shared/EditorTemplates/Package.cshtml
. Those are the 2 locations that ASP.NET MVC will look for it in that order.
Upvotes: 1
Reputation: 17485
As per your requirement i understand that you don't want to use UIHint or any other way that say which template to use.
This may work for you.
public class PackageCollection : List<JYP.Business.ViewModels.Package>
{
}
Now In Main Model use
public class ModelTest{
public PackageCollection Items { get; set;}
public ModelTest(){
Items = new PackageCollection();
}
}
Now create EditorTemplate with Name PackageCollection.cshtml or PackageCollection.ascx as per your ViewEngine.
Another Solution
Now if you do not wan't to use UIHint and above solution then you have to specify TemplateName in Editor For
Html.EditorFor(model=>model.Items , "yourtemplatename")
This works without creating packagecollection class.
Thanks.
Upvotes: 0