并非所有代码路径都在Controller中返回值

Tim*_*ong -1 c# asp.net-mvc asp.net-mvc-4

我似乎无法弄清楚为什么我的[HttpPost]Index()方法给我一个错误信息说明

并非所有代码路径都返回一个值

我尝试了return View()之后AddModelError但仍然给了我错误信息.

public class SnowboardController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(SnowboardModel SbModel)
    {
        if (SbModel.DiscountSenior && SbModel.DiscountStudent)
        {
            ModelState.AddModelError("Discounts", "Dude, you cannot take both student and senior discounts.");
        }
        //return to view if any fields invalid
        if (!ModelState.IsValid)
        {
            return View(SbModel);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我还在@Html.ValidationMessage("Discounts")我的观点中补充道.

Sle*_*mer 5

在Index方法中,所有返回都包含在ifs中...您需要在方法结束时返回.