locked
Get values from virtual properties RRS feed

  • Question

  • If I have this class below:

    public class Product
        {
            public int ProductId { get; set; }
            public virtual List<Author> Author { get; set; }

    How can I get it's values on razor aspx file ?

    <span>@Model.WhatsNew1.ProductId <br />@Model.WhatsNew1.Author.FirstName  @Model.WhatsNew1.Author.LastName</span>

    Friday, August 8, 2014 6:09 PM

Answers

  • I don't think your problem is related to the fact that the property is virtual. The problem is that the property type is List<Author> and you're trying to access it as ifs type was just Author.

    Since it's a list you'll probably want to display all the authors in it. For details on how you might do that please use the MVC forum: http://forums.asp.net/1146.aspx/1?MVC

    • Marked as answer by AGA Neto Monday, August 11, 2014 9:52 PM
    Saturday, August 9, 2014 5:59 AM
  • Provided that the Author list has actually been populated with Author objects in the controller, you get the values from each Author object in the list by iterating through it in the view (remember that the Author property is a List that may contain several objects!):

    @foreach(var author in Model.WhatsNew1.Author)
    {
    <span>@author.ProductId <br />@author.FirstName  @author.LastName</span>
    }

    But please ask ASP.NET questions here: http://forums.asp.net.

    • Marked as answer by AGA Neto Monday, August 11, 2014 9:49 PM
    Saturday, August 9, 2014 10:19 AM

All replies

  • I don't think your problem is related to the fact that the property is virtual. The problem is that the property type is List<Author> and you're trying to access it as ifs type was just Author.

    Since it's a list you'll probably want to display all the authors in it. For details on how you might do that please use the MVC forum: http://forums.asp.net/1146.aspx/1?MVC

    • Marked as answer by AGA Neto Monday, August 11, 2014 9:52 PM
    Saturday, August 9, 2014 5:59 AM
  • Provided that the Author list has actually been populated with Author objects in the controller, you get the values from each Author object in the list by iterating through it in the view (remember that the Author property is a List that may contain several objects!):

    @foreach(var author in Model.WhatsNew1.Author)
    {
    <span>@author.ProductId <br />@author.FirstName  @author.LastName</span>
    }

    But please ask ASP.NET questions here: http://forums.asp.net.

    • Marked as answer by AGA Neto Monday, August 11, 2014 9:49 PM
    Saturday, August 9, 2014 10:19 AM