I am trying to work on an asp.net mvc5 project where I have two Post and Game models.
It will work like this, you publish a message, and you choose which game you made and also choose which game you are going to do next. As you can see in this image, I got it so that you got a DropDownList from the games that are in the database:

As always, when I click Create, I get an error. The value "1" is invalid.

And I'm not sure why this will happen, as in my database. I save these values as int.
Post Model:
public class Post
{
[Key]
public int PostId { get; set; }
[Display(Name = "URL")]
[StringLength(80)]
[DataType(DataType.Url)]
[Required]
public string Url { get; set; }
[Display(Name = "User")]
public virtual ApplicationUser User { get; set; }
[Display(Name = "Game")]
public virtual Game GameId { get; set; }
[Display(Name = "Next Game")]
public virtual Game NextGameId { get; set; }
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;
}
}
}
Mail Controller:
public ActionResult Create()
{
ViewBag.GameId = new SelectList(db.Games, "GameId", "GameTitle");
ViewBag.NextGameId = new SelectList(db.Games, "GameId", "GameTitle");
return View();
}
[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);
}
Game Model:
public class Game
{
[Key]
public int GameId { get; set; }
public string GameTitle { get; set; }
}
And my Messages / Create view , where I create drop-down menus:
<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>