Reputation: 345
Hi people I have the following code:
public ActionResult Create(GameTBL gametbl)
{
if (ModelState.IsValid)
{
//First you get the gamer, from GamerTBLs
var gamer = db.GamerTBLs.Where(k => k.UserName == User.Identity.Name).SingleOrDefault();
//Then you add the game to the games collection from gamers
gamer.GameTBLs.Add(gametbl);
db.SaveChanges();
return RedirectToAction("Index");
}
}
It is giving me the following error:
Error 1 'MvcApplication1.Controllers.GameController.Create(MvcApplication1.Models.GameTBL)': not all code paths return a value
What this code is trying to do is trying to populate the foreign key of gamer into the Game Table
Model for my controller Gamer:
public string UserName { get; set; }
public int GamerID { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }
public string DOB { get; set; }
public string BIO { get; set; }
Model for my Game Controller:
public int GameID { get; set; }
public string GameName { get; set; }
public string ReleaseYear { get; set; }
public string Cost { get; set; }
public string Discription { get; set; }
public string DownloadableContent { get; set; }
public string Image { get; set; }
public string ConsoleName { get; set; }
public int GamerIDFK { get; set; }
public byte[] UserName { get; set; }
Upvotes: 0
Views: 258
Reputation: 4770
Just so you know, the error isn't really ASP.Net MVC related - it would be an error in any method that returns a value.
The error message not all code paths return a value
means just that - there is a path through the code that doesn't return a value, when the method signature says that it should.
In your case, your action method has the signature ActionResult Create(GameTBL gametbl)
so all paths through the method have to return an ActionResult
. In your code, the path that occurs when ModelState.IsValid
is true does return an ActionResult
- but nothing is returned in the path where ModelState.IsValid
is false.
The other answers give you examples on how to correct your code by returning an ActionResult through the 'ModelState.IsValid
is false' path.
Upvotes: 0
Reputation: 1857
try this...the return statement should be outside the if statement...the issue is you are not returning a view/action result when the modelstate is not valid...
public ActionResult Create(GameTBL gametbl)
{
if (ModelState.IsValid)
{
//First you get the gamer, from GamerTBLs
var gamer = db.GamerTBLs.Where(k => k.UserName == User.Identity.Name).SingleOrDefault();
//Then you add the game to the games collection from gamers
gamer.GameTBLs.Add(gametbl);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(gametbl);
}
Upvotes: 0
Reputation: 590
You just need to return a view when your ModelState isn't valid.
public ActionResult Create(GameTBL gametbl)
{
if (ModelState.IsValid)
{
//First you get the gamer, from GamerTBLs
var gamer = db.GamerTBLs.Where(k => k.UserName == User.Identity.Name).SingleOrDefault();
//Then you add the game to the games collection from gamers
gamer.GameTBLs.Add(gametbl);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(gametbl);
}
This will make the page show any errors in model creation (assuming you have validation).
Upvotes: 3