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

問題は変数のスコープにあります - myID は IF ブロックで宣言されており、それを ELSE ブロックで使用しようとしています。

else ブロックは、この変数について認識していません。これは、表示されるエラー メッセージです。

于 2013-05-22T17:03:54.990 に答える
0

myId は、else のように if ブロックの外側で定義する必要があります。宣言していません。代わりにこれを試してください、バージョン 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 に答える