Hi.. I'm new to .net & mvc and I've been trying to build my first app just to get a feel of things.. But I've hit a small issue..
I have a book class, like so:
public class Book {
public int BookId { get; set; }
[Required(ErrorMessage="Please enter a title")]
[MaxLength(200)]
public string Title { get; set; }
[Required(ErrorMessage="Please enter a price")]
[Range(0, Double.MaxValue)]
public double Price { get; set; }
[Display(Name="Display on site?")]
public bool Display { get; set; }
public Book() {
Price = 0;
Display = true;
}
}
And I have an Author class like this:
public class Author {
[Key]
public int AuthorId { get; set; }
[Required(ErrorMessage="Please enter an authors name")]
public string AuthorName { get; set; }
public virtual List<Book> Books { get; set; }
}
I assume this is the correct way to have many books be related to ONE author (likewise having ONE author be linked to many books).
My problem comes when displaying the book information. As well as all the information in the book class, I also wish to display the a list of authors, with actual author of the book selected - which the user can change.
I can't exectly figure out the best way to do this.. I assume it is related to a viewModel and more complex Lambda expression that I'm used to.. Can anyone point me in the right direction.. Thanks :)
well you can add an int AutorId property to your Book class and use it to select the proper author( of course the Book sql table must have a column with the AuthorId for this purpouse)
and you can add a list of Authors type to your Book class (if needed) to populate the authors select( i don't know what this select is made for).
public class Author {
[Key]
public int AuthorId { get; set; }
[Required(ErrorMessage="Please enter an authors name")]
public string AuthorName { get; set; }
public virtual List<Book> Books { get; set; }
}
public class Book {
public Book() {
this.Price = 0;
this.Display = true;
}
[Key]
public int BookId { get; set; }
[DisplayName("Author")]
public int AuthorId { get; set; }
[Required(ErrorMessage="Please enter a title")]
[MaxLength(200)]
public string Title { get; set; }
[Required(ErrorMessage="Please enter a price")]
[Range(0, Double.MaxValue)]
public double Price { get; set; }
[Display(Name="Display on site?")]
public bool Display { get; set; }
public virtual Author Author { get; set; }
}
when you querying books, you will also be able to access its Author information:
//BookController.cs:
public ActionResult Index() {
var books = db.Books.ToList();
return View( books );
}
//View file.cshtml:
@model IEnumerable<Book>
<table>
<tr>
<th>book</th>
<th>Author</th>
</tr>
@foreach( var item in Model){
<tr>
<td>@item.Title</td>
<td>@item.Author.AuthorName</td>
</tr>
}
</table>
Drummer_si
0 Points
1 Post
ViewModel Question? (Obtaining author of a book)
Jul 04, 2012 09:55 PM|LINK
Hi.. I'm new to .net & mvc and I've been trying to build my first app just to get a feel of things.. But I've hit a small issue..
I have a book class, like so:
public class Book { public int BookId { get; set; } [Required(ErrorMessage="Please enter a title")] [MaxLength(200)] public string Title { get; set; } [Required(ErrorMessage="Please enter a price")] [Range(0, Double.MaxValue)] public double Price { get; set; } [Display(Name="Display on site?")] public bool Display { get; set; } public Book() { Price = 0; Display = true; } }And I have an Author class like this:
public class Author { [Key] public int AuthorId { get; set; } [Required(ErrorMessage="Please enter an authors name")] public string AuthorName { get; set; } public virtual List<Book> Books { get; set; } }I assume this is the correct way to have many books be related to ONE author (likewise having ONE author be linked to many books).
My problem comes when displaying the book information. As well as all the information in the book class, I also wish to display the a list of authors, with actual author of the book selected - which the user can change.
I can't exectly figure out the best way to do this.. I assume it is related to a viewModel and more complex Lambda expression that I'm used to.. Can anyone point me in the right direction.. Thanks :)
CPrakash82
All-Star
18154 Points
2831 Posts
Re: ViewModel Question? (Obtaining author of a book)
Jul 04, 2012 10:37 PM|LINK
Look for the tutorials on this site they will help you.
www.asp.net/mvc/tutorials
Thanks,
MegaByteMe
Member
10 Points
9 Posts
Re: ViewModel Question? (Obtaining author of a book)
Jul 04, 2012 11:43 PM|LINK
well you can add an int AutorId property to your Book class and use it to select the proper author( of course the Book sql table must have a column with the AuthorId for this purpouse)
and you can add a list of Authors type to your Book class (if needed) to populate the authors select( i don't know what this select is made for).
.NetBoy
Member
439 Points
115 Posts
Re: ViewModel Question? (Obtaining author of a book)
Jul 05, 2012 07:49 AM|LINK
public class Author { [Key] public int AuthorId { get; set; } [Required(ErrorMessage="Please enter an authors name")] public string AuthorName { get; set; } public virtual List<Book> Books { get; set; } } public class Book { public Book() { this.Price = 0; this.Display = true; } [Key] public int BookId { get; set; } [DisplayName("Author")] public int AuthorId { get; set; } [Required(ErrorMessage="Please enter a title")] [MaxLength(200)] public string Title { get; set; } [Required(ErrorMessage="Please enter a price")] [Range(0, Double.MaxValue)] public double Price { get; set; } [Display(Name="Display on site?")] public bool Display { get; set; } public virtual Author Author { get; set; } }when you querying books, you will also be able to access its Author information:
//BookController.cs: public ActionResult Index() { var books = db.Books.ToList(); return View( books ); } //View file.cshtml: @model IEnumerable<Book> <table> <tr> <th>book</th> <th>Author</th> </tr> @foreach( var item in Model){ <tr> <td>@item.Title</td> <td>@item.Author.AuthorName</td> </tr> } </table>this is an example
MegaByteMe
Member
10 Points
9 Posts
Re: ViewModel Question? (Obtaining author of a book)
Jul 05, 2012 05:19 PM|LINK
you may wanto to add the include method
var books = db.Books.include( [Author lamba exp.] )ToList();
This way you ensure you you get the book and author instantiated.
vogelra
Member
44 Points
20 Posts
Re: ViewModel Question? (Obtaining author of a book)
Jul 10, 2012 03:13 AM|LINK
.NetBoy
Member
439 Points
115 Posts
Re: ViewModel Question? (Obtaining author of a book)
Jul 10, 2012 05:03 AM|LINK
If you have found one of the answers as correct OR resolving your issue, don't forget to Mark As Answer