Use Interfaces
Check it out, your View expects to receive a list of objects where they necessarily have the property Name
:
@foreach (var item in Model)
{
<h1>@item.name</h2>
}
So we created a contract to make it clear to these objects that they are required to implement this property, which also View
that any object in that list will have a property Name
.
public interface ITemQueTerPropName
{
string Name { get; set; }
}
Now tell this to your View, so she will behave as expected:
@model List<ITemQueTerPropName>
@foreach (var item in Model)
{
<h1>@item.Name</h2>
}
And don’t forget to implement the interface in the relevant objects:
public class UmObjetoQueIraAparecerNaView : ITemQueTerPropName
{
public string Name { get; set; }
}
So his View
will behave as expected, and will also ensure that your view will only render objects that have the property Name
.
The way you did not... What’s the point of this?
– novic
But is there any way to get this result? Probably not?
– Bruno Heringer
Goal is to use it as partial view... which will list the object of the list. (Independent of the type of the list)
– Bruno Heringer
Bruno to
View
needs to know the type you are sending, that way aView
with the Generic type would not give... only if it were aView
with the same fields– novic
@Brunoheringer you want to create a partial view to save 5 lines of code??
– Jéf Bueno
@LINQ think he wants to save on files
cshtml
.– Thiago Lunardi