I'm trying to get better with asp.net mvc5 how ever I've ran into a problem. I've made Gamemodel where you can create games. On my post/create I get a drop down list for the games I've added on my Game model.
I also have my 'Title / Next game' I want a drop-down list on that as-well. But right now it just takes a string, but I would like a drop-down list like for GameId how can I achieve this ?
This is my Post Model
public class Post
{
[Key]
public int PostId { get; set; }
//URL
[Display(Name = "URL")]
[StringLength(80)]
public string Url { get; set; }
//User
[Display(Name = "User")]
public virtual ApplicationUser User { get; set; }
//Game
[Display(Name = "Game")]
public int GameId { get; set; }
[Display(Name = "Next Game")]
public string Title { get; set; }
public virtual Game Game { get; set; }
//Time
private DateTime? _date;
public DateTime? Date
{
get
{
if (_date == null || _date.ToString() == "1/1/0001 12:00:00 AM")
{
return _date = DateTime.Now;
}
return _date;
}
set
{
_date = value;
}
}
And this is my Game Model
public class Game
{
[Key]
public int GameId { get; set; }
//Game
[Display(Name = "Game")]
public string Title { get; set; }
//User
[Display(Name = "User")]
public virtual ApplicationUser User { get; set; }
}
I would remove the gameid and game title properties from the post model. Then I would change the virtual game property to be a collection so that you can get all gameids and titles from the game model. You should try not to duplicate properties. Use something like public virtual List on the post model
If I understood correctly, you would like to populate a dropdown via MVC Helper. You can use IEnumerable or List if you prefer of type SelectListItem instead of string in the model. The SelectListItem object has properties such as key (this is the id), value which is the value name, select, disabled and etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.