Let's say this is my model:
public class Book
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Pages { get; set; }
}
and this is model 2
public class Author
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
}
In any view, you must have one model @model Book
, but I want to get a property from Author
class in Book
view.
Note: single-value prop - not list
CodePudding user response:
You can create a ViewModel to contains Book
and Author
public class ViewModel
{
public Book book{ get; set; }
public Author author{ get; set; }
}
Use this model to pass data between view and controller. Then you can call props from both Book
and Author
in the same View.
More details you can refer to this Docs.
CodePudding user response:
add navigation property to Book
Book {
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Pages { get; set; }
public int AuthorID {get; set; }
public virtual Author Author {get; set; }
}