Problem Description:
I am using ASP.NET MVC and I have the following method in my Controller Class. This method uses SelectList method to select a list of items from the database. These items will then be passed to the view to be displayed in a drop down list.
public ActionResult Edit(int id)
{
Album album = db.Albums.Find(id);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
}
My Understanding of the selectMethod:
I know that the first param takes a list of items.
The third param is the property to be displayed.In this case, we will be displaying the name property of the Artists.
Based on my research, I found out that the 4th param is the default value that will be displayed on the dropdown list.
My Question:
1) I would like someone to help me understand about the second param.
2)Since we are displaying names of the artists, how can we display a default artist name in the fourth param using Artist ID?
I hope you guys understood my questions. I would be happy to clarify them to you if you need me to do so.
db.Artistsincludes an artist whoseArtistIdproperty matches the given value, the rendered HTML will produce a select list where that artist is selected.Did that answer your questions?