0

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; }
user1137472
  • 345
  • 2
  • 5
  • 20

3 Answers3

3

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).

Brent Echols
  • 590
  • 2
  • 9
  • Still not populating the foreign key from the gamer table to the game table – user1137472 Mar 26 '12 at 23:51
  • Are any errors thrown? Or is it just not saving? Can you enter a breakpoint and ensure that gamer is being set, and then make sure that gametbl is being added to gamer? How in depth of debugging have you done? – Brent Echols Mar 27 '12 at 15:02
0

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);
    }
NiK
  • 1,827
  • 1
  • 20
  • 37
  • Still not populating the foreign key from the gamer table to the game table – user1137472 Mar 26 '12 at 23:50
  • @user1137472 if I understand correctly you are trying to populate "GamerIDFK" property of your controller model? can you post your view code to see what you are doing with this GamerIDFK property in the view... – NiK Mar 27 '12 at 14:59
0

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.

StanK
  • 4,750
  • 2
  • 22
  • 46
  • That is all fine of what you have stated but i still have the problem populating the foreign key of gamer into game, when i run my application the foreign key is not being populated – user1137472 Mar 26 '12 at 23:49
  • My answer (and other users) explains how to fix your error message. As to why your foreign key isn't saving - this a different issue and will be related to your Data Access code – StanK Mar 27 '12 at 00:31