0

错误在 Create() 函数中

  • 创造()

  • 错误 1 ​​'MvcAnketaIT.Controllers.SurveyController.Create()':并非所有代码路径都返回值

代码

public ActionResult Create()
    {
        int myId = getIdByUser(this.User.Identity.Name);
        if (this.User.Identity.IsAuthenticated)
        {

            if (myId == -1) //no questionnaire in db           
            {
                     SurveyModel survey = new SurveyModel();
                     ViewBag.userName = this.User.Identity.Name;
                     ViewBag.Q3Sex = new SelectList(db.Genders, "ID", "gender");
                     ViewBag.Q8Question1 = new SelectList(db.Questions1, "Id", "Technology");
                     ViewBag.Q9Question2_1 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q10Question2_2 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q11Question2_3 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q12Question2_4 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q13Question2_5 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q14Question2_6 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q15Question2_7 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q16Question2_8 = new SelectList(db.Satisfaction, "ID", "Name");
                     ViewBag.Q17Question3 = new SelectList(db.Questions3, "ID", "Solve");
                     ViewBag.Q19Question5 = new SelectList(db.Questions5, "ID", "No_Contacts");
                     ViewBag.Q20Question6 = new SelectList(db.Questions6, "ID", "Recommendation");
                     return View();

            }
        }

         else //user already has a questionnaire fulfilled
            {
                return RedirectToAction("Edit/" + myId); //redirect to the right id of the user
            }
    }
4

3 回答 3

1

您在if块内声明一个变量:

if (something)
{
    int myId = getIdByUser(this.User.Identity.Name);
}
else
{
    // myId doesn't exist here.
}

因此该变量只能在该块的范围内访问。要在更大的范围内使用它,需要在块之外声明它:

int myId;
if (something)
{
    myId = getIdByUser(this.User.Identity.Name);
}
else
{
    // myId is accessible here
    // though runs the risk of not having been set to anything
    // depending on whether or not the if condition was met
}

编辑:您添加到问题中的是一个完全不同的问题。不过,信息很明确。并非所有代码路径都返回值。你所拥有的结构可以简化为:

if (something)
{
    if (something_else)
    {
        return;
    }
}
else
{
    return;
}

如果something是真的,但something_else不是,会发生什么?return从未达成任何声明。因此,该函数没有返回任何内容。这会导致编译器错误。您需要从每个代码路径中的函数返回一些内容。在这种情况下,这里:

if (something)
{
    if (something_else)
    {
        return;
    }
    // Need to return a value here
}
else
{
    return;
}
于 2013-05-22T17:01:10.123 回答
1

问题在于变量的范围 - myID 在 IF 块中声明,您正试图在 ELSE 块中使用它。

else 块不知道此变量,这是您收到的错误消息。

于 2013-05-22T17:03:54.990 回答
0

myId 必须在 if 块之外定义,就像在 else 中一样,你还没有声明它。试试这个,版本 2:

public ActionResult Create()
{
    int myId = getIdByUser(this.User.Identity.Name);
    if (this.User.Identity.IsAuthenticated)
    {
        if (myId == -1) //no questionnaire in db           
        {
            SurveyModel survey = new SurveyModel();          
            return View();         
        }
    }   
    else //user already has a questionnaire fulfilled
    {
        //redirect to the right id of the user
        return RedirectToAction("Edit/" + myId); 
    }
    return View();
}
于 2013-05-22T17:04:27.017 回答