Suppose the following domain model.
public class SystemRequirement
{
public int SystemRequirementID { get; set; }
public Platforms Platform { get; set; }
public string CPU { get; set; }
public string RAM { get; set; }
public string Dsiplay { get; set; }
public string Discription { get; set; }
public int GameID { get; set; }
public virtual Game Game { get; set; }
}
public class Game
{
public int GameID { get; set; }
public string Name { get; set; }
public Genres Genre { get; set; }
public DateTime ReleaseDate { get; set; }
public bool Multiplay { get; set; }
public string About { get; set; }
public virtual List<SystemRequirement> SystemRequirements { get; set; }
}
public class GameSystemViewModel
{
public Game Game { get; set; }
public SystemRequirement SystemRequirement { get; set; }
}
I want to make make a form which allows to add a new game which contains its system requirement too.
@model KeyStore.WebUI.Models.GameSystemViewModel
@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<div class="container">
@using (Html.BeginForm("Add", "GameAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="panel">
<div class="panel-heading">
<h3>Add New Game</h3>
</div>
<div class="panel-body">
@Html.LabelFor(m => m.Game.Name)
@Html.TextBoxFor(m => m.Game.Name)
@Html.LabelFor(m => m.Game.Genre)
@Html.TextBoxFor(m => m.Game.Genre)
@Html.LabelFor(m => m.Game.ReleaseDate)
@Html.TextBoxFor(m => m.Game.ReleaseDate)
@Html.LabelFor(m => m.Game.Multiplay)
@Html.CheckBoxFor(m => m.Game.Multiplay)
@Html.LabelFor(m => m.Game.About)
@Html.TextAreaFor(m => m.Game.About)
</div>
</div>
<div class="panel">
<div class="panel-heading">
<h3>System Requirements</h3>
</div>
<div class="panel-body">
@Html.LabelFor(m => m.SystemRequirement.Platform)
@Html.TextBoxFor(m => m.SystemRequirement.Platform)
@Html.LabelFor(m => m.SystemRequirement.CPU)
@Html.TextBoxFor(m => m.SystemRequirement.CPU)
@Html.LabelFor(m => m.SystemRequirement.RAM)
@Html.TextBoxFor(m => m.SystemRequirement.RAM)
@Html.LabelFor(m => m.SystemRequirement.Dsiplay)
@Html.TextBoxFor(m => m.SystemRequirement.Dsiplay)
@Html.LabelFor(m => m.SystemRequirement.Discription)
@Html.TextAreaFor(m => m.SystemRequirement.Discription)
</div>
</div>
<div class="panel-footer">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
}
</div>
My controller class has these three actions.
- Add: to make game view
- [HttpPost]Add: to get the object and save them in a db using linq
Controller code:
public ViewResult Add()
{
GameSystemViewModel g = new GameSystemViewModel();
g.Game = new Game();
g.SystemRequirement = new SystemRequirement();
return View(g);
}
[HttpPost]
public ActionResult Add(GameSystemViewModel g)
{
Game mygame = new Game();
mygame = g.Game;
mygame.SystemRequirements.Add(g.SystemRequirement);
gameRepository.Save(mygame);
return RedirectToAction("Add");
}
but when the form is posted the following error occurs. As you see in Locals SystemRequirement is NOT NULL.
What is the problem and how can I fix it?