-3

I keep getting this error, and I don't know how to fix it:

CS0161:Home.Controller.Index(): not all code paths return a value

using Microsoft.AspNetCore.Mvc;
using TipCalculator.Models;

namespace TipCalculator.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public IActionResult Index()      // the Index is underline in red
        {
            ViewBag.Fifteen = 0;
            ViewBag.Twenty = 0;
            ViewBag.TwentyFive = 0;     
            View();
        }

        [HttpPost]
        public IActionResult Index(Calculator calc)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Fifteen = calc.CalculateTip(0.15);
                ViewBag.Twenty = calc.CalculateTip(0.20);
                ViewBag.TwentyFive = calc.CalculateTip(0.25);
            }
            else
            {
                ViewBag.Fifteen = 0;
                ViewBag.Twenty = 0;
                ViewBag.TwentyFive = 0;
            }

            return View(calc);
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ta H
  • 1

1 Answers1

3

**CS0161:Home.Controller.Index(): not all code paths return a value

To help you understand this message. Above error message means Index() method in HomeController does not return a value. Looking at Index() method I see return statement is missing. Add a return statement to Index() method to eliminate this error. Replace you index method with following method. This is exact copy of your method but with last statement View(); replaced with return View();.

[HttpGet]
public IActionResult Index()      // the Index is underline in red
{
    ViewBag.Fifteen = 0;
    ViewBag.Twenty = 0;
    ViewBag.TwentyFive = 0;     
    return View();
}
NCCSBIM071
  • 1,207
  • 1
  • 16
  • 30