4

I am new to mvc C# and am stuck. Please advise on how to fix this. I'm getting the error on Add. When I hover over the red squiggly line it says "Not all code paths return a value"

    public ActionResult Add(ShapeInputModel dto, FormCollection collection)
    {

        var model = new GeoRegions();

        if (TryUpdateModel(model))
        {


            var destinationFolder = Server.MapPath("/App_Data/KML");
            var postedFile = dto.Shape;

            if (postedFile != null)
            {
                var fileName = Path.GetFileName(postedFile.FileName);
                var path = Path.Combine(destinationFolder, fileName);
                postedFile.SaveAs(path);

                //Save to Database
                Db.AddGeoRegions(model);
                return RedirectToAction("Index");

            }

            return View();

        }
    }
Icarus
  • 63,293
  • 14
  • 100
  • 115
user1382770
  • 273
  • 2
  • 4
  • 10
  • Possible duplicate of [c# returning error "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value) – Liam Jun 20 '18 at 09:15

10 Answers10

12

Use This :

public ActionResult Add(ShapeInputModel dto, FormCollection collection)
{
    var model = new GeoRegions();

    if (TryUpdateModel(model))
    {
        var destinationFolder = Server.MapPath("/App_Data/KML");
        var postedFile = dto.Shape;

        if (postedFile != null)
        {
            var fileName = Path.GetFileName(postedFile.FileName);
            var path = Path.Combine(destinationFolder, fileName);
            postedFile.SaveAs(path);

            //Save to Database
            Db.AddGeoRegions(model);
            return RedirectToAction("Index");
        }
        return View();

    }
    return null; // you can change the null to anything else also.
}

The error happens because your functions doesn't return anything if TryUpdateModel(model) = false. So adding the line return null or return 'any other thing' will solve the problem!

Community
  • 1
  • 1
Writwick
  • 2,133
  • 6
  • 23
  • 54
  • Thank you to everyone for your answers. I have learned a lot from all of you and I truly appreciate it, though I still have so much to learn. I used return null and everything is working perfectly! – user1382770 May 24 '12 at 13:14
  • Remember, If you use `null`, you should always check the value, if it is null or not when returned by the Method! – Writwick May 24 '12 at 16:27
3

There is no return if the "if" is never entered.

I like to always keep if-else's used [with return] balanced to I can see the return values (and that all paths have a return value) at a glance:

if (TryUpdateModel(model))
{
    ...
    if (postedFile != null)
    {
        ...
        return RedirectToAction("Index");
    } else {
        return View();
    }
} else {
    return View(); // or null/whatever is appropriate
}

Of course ReSharper often tells me I have "useless" else statements ;-)

Happy coding.

2

Add

 return null;

before the last line }

Tilak
  • 30,108
  • 19
  • 83
  • 131
0

Nothing is being returned if (TryUpdateModel(model)) returns false. Maybe you meant to have your return View(); outside of the if?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
0

The error does what it says on the tin; there's a code path where the function will complete but will not return a value. A function that has a return type must always either return a value or throw an exception.

In your case, if TryUpdateModel(model) returns false, you have no return value.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

Well read the error! At some point in your method's execution, you must return a value, or throw an exception. (I think that returning null is in order in this case)

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
0

Certainly, your initial if (TryUpdateModel(model)) makes your routine only return a value when the condition is true; if it isn't, nothing will be returned, which violates the method signature.

Icarus
  • 63,293
  • 14
  • 100
  • 115
0

You have

if (TryUpdateModel(model))
{
    // lots of stuff

    return View();
}

So what will be returned if TryUpdateModel is not true?

Your method must return an ActionResult even when the if statement is false.

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
0

Just having a quick look at your code I can see that you have your return command(return View() inside the "if" statement block. Now if the "If" condition was to fail there is no return statement outside it's scope. The easiest way would be to

 }

            return View();

        }
return null; // Depends upon your code though. you might want to return something else
}
Abhi.Net
  • 722
  • 3
  • 11
  • 37
0

You are returning value inside "if()" condition. What suppose when if condition fails?? the program will wont return value. So, return any default value outside if condition, it may be in else condition.

public ActionResult Add(ShapeInputModel dto, FormCollection collection)
{

    var model = new GeoRegions();

    if (TryUpdateModel(model))
    {
     ....
     ....
    }
    return default_value;//it may be in else condition also.
}

Try it. And if ur problem is solved then mark as answered. So that it is useful to others.

Syed Yunus
  • 198
  • 3
  • 4
  • 14