【发布时间】:2015-06-30 10:53:44
【问题描述】:
我正在尝试处理 asp.net mvc5 项目,其中我有两个模型 Post 和 Game。
它会像这样工作,您在 Post 上发帖并选择您制作的游戏,并且您还可以选择接下来要制作的游戏。正如你在这张图片上看到的那样,我得到了数据库中游戏的 DropDownList:
当我按下创建时,我得到一个错误,说值“1”是无效的。
我不确定为什么会发生这种情况,因为在我的数据库中我将这些值保存为 int's.Post Model:
public class Post
{
[Key]
public int PostId { get; set; }
//URL
[Display(Name = "URL")]
[StringLength(80)]
[DataType(DataType.Url)]
[Required]
public string Url { get; set; }
//User
[Display(Name = "User")]
public virtual ApplicationUser User { get; set; }
//Game
[Display(Name = "Game")]
public virtual Game GameId { get; set; }
[Display(Name = "Next Game")]
public virtual Game NextGameId { 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;
}
}
}
后控制器:
// GET: Posts/Create
public ActionResult Create()
{
ViewBag.GameId = new SelectList(db.Games, "GameId", "GameTitle");
ViewBag.NextGameId = new SelectList(db.Games, "GameId", "GameTitle");
return View();
}
// POST: Posts/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "PostId,Url,GameId,NextGameId,Date")] Post post)
{
if (ModelState.IsValid)
{
db.Posts.Add(post);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.GameId = new SelectList(db.Games, "GameId", "GameTitle", post.GameId);
ViewBag.NextGameId = new SelectList(db.Games, "GameId", "GameTitle", post.NextGameId);
return View(post);
}
游戏模型:
public class Game
{
[Key]
public int GameId { get; set; }
public string GameTitle { get; set; }
}
还有我创建下拉列表的帖子/创建视图:
<div class="form-group">
@Html.LabelFor(model => model.GameId, "GameId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("GameId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.GameId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NextGameId, "NextGame", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("NextGameId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.NextGameId, "", new { @class = "text-danger" })
</div>
</div>
【问题讨论】:
-
按你说的工作... 提示:
[Display(Name = "Game")] public virtual Game GameId { get; set; }应该是int?而不是Game -
你为什么从之前的问题中改变你的模型 -
GameId和NextGameId属性现在是 typeofGame(一个复杂对象)。将它们改回int。并且不要将您的ViewBag属性命名为与模型属性相同 -
调用导航属性
GameId和NextGameId很混乱。 -
@StephenMuecke 因为在上一个问题中确实有人提到这样做会更好,所以我尝试了但失败了。
-
将属性改回
int。在控制器中使用ViewBag.GameList = new SelectList(db.Games, "GameId", "GameTitle")(只需要一个),在视图中使用@Html.DropDownListFor(m => m.GameId, (SelectList)ViewBag.GameList , new { @class = "form-control" }),NextGameId同上
标签: c# asp.net asp.net-mvc-5