填充mvc5列表? [英] Populate a mvc5 list?
问题描述
现在我可以创建游戏在游戏/创建。
而且我可以发表下一个职位/创建。
但我的问题是,现在我的公众诠释游戏ID {搞定;组; }
获取与那就是在我的游戏表我的SQL数据库中的项目填充。这是蛮好这就是我喜欢它。
不过,我想公共字符串名称{搞定;组; }
来与那些在游戏数据库中的值填充相同的方式。
公众诠释游戏ID {搞定;组; }
被填充带是在我的游戏表中的值一个DropDownList。
至于公共字符串名称{搞定;组; }
我必须手动输入。我想,要填充的方式为游戏ID
这是我的帖子型号
公共类帖子
{
[键]
公众诠释{帖子ID获取;组; } // URL
[显示(名称=URL)]
[StringLength(80)]
公共字符串网址{搞定;组; }
//用户
[显示(名称=用户)]
公共虚拟ApplicationUser用户{搞定;组; } //游戏
[显示(名称=游戏)]
公众诠释游戏ID {搞定;组; }
[显示(NAME =下一场比赛)]
公共字符串名称{搞定;组; }
公共虚拟游戏游戏{搞定;组; } //时间
私人的DateTime? _日期;
公众的DateTime?日期
{
得到
{
如果(_date == NULL || _date.ToString()==1/1/0001 12:00:00 AM)
{
返回_date = DateTime.Now;
}
归期;
}
组
{
_date =价值;
}
}
这是我的博弈模型
公共类游戏
{
[键]
公众诠释游戏ID {搞定;组; } //游戏
[显示(名称=游戏)]
公共字符串名称{搞定;组; } //用户
[显示(名称=用户)]
公共虚拟ApplicationUser用户{搞定;组; }
}
其实你不需要在安置自己的型号你的财产的标题,在你的控制器,你可以做这样的事情来获得游戏中的所有数据。
使用(VAR DB =新YourDbContext())
{
无功后= db.Posts()包括(G => g.Game)。.FirstOrDefault();
VAR gameTitle = post.Game.Title;
}
Right now I can create games under the Games/Create. And I can make a post under Post/Create.
But my problem is that right now my public int GameId { get; set; }
gets populated with the items that is in my Game table in my SQL database. Which is just fine that's how I like it.
But I want public string Title { get; set; }
to the populated the same way with the values that are in the Game database.
public int GameId { get; set; }
gets populated with a dropdownlist with the values that is in my Game table.
And as for public string Title { get; set; }
I have to enter it manually. I want that to be populated the same way as GameId
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; }
}
Actually you don't need your property Title in your Post Model, in your controller you can do something like this to get all data of Game.
using (var db = new YourDbContext())
{
var post = db.Posts().Include(g => g.Game).FirstOrDefault();
var gameTitle = post.Game.Title;
}
这篇关于填充mvc5列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!