Right now I have a MVC5 project that looks like this when creating a post:
So I do get a Drop down lsit for both GameId and NextGame which is how I want it. How ever when creating this post only 'GameId' gets saved to the database. While the NextGame Tab would be left at null. Any idea on how I would go a head and fix this?
This is my 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 int GameId { get; set; }
public string NextGame { 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 here is my PostModelCreate method
// GET: Posts/Create
public ActionResult Create()
{
ViewBag.GameId = new SelectList(db.Games, "GameId", "Title");
ViewBag.NextGame = new SelectList(db.Games, "NextGame", "Title");
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,NextGame,Date")] Post post)
{
if (ModelState.IsValid)
{
db.Posts.Add(post);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.GameId = new SelectList(db.Games, "GameId", "Title", post.GameId);
ViewBag.NextGame = new SelectList(db.Games, "NextGame", "Title", post.NextGame);
return View(post);
}
And this is my view where I create the dropdown lists
<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.NextGame, "NextGame", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("NextGame", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.NextGame, "", new { @class = "text-danger" })
</div>
</div>