2017-07-26 74 views
-1

我得到一個回報的創建頁面上提交POST時查看和調試已經把範圍縮小到沒有被有效的模型的狀態,但我不知道爲什麼它是無效的。模型狀態無效,但爲什麼?

這是我正在做的。我添加了一個上傳的文件,在創建方法中工作正常,直到我需要從下拉菜單添加更多細節。最初它看起來像這樣當所有我需要的文件:

public ActionResult Create(HttpPostedFileBase file) 
     { 
      ViewBag.EnvironmentTypeId = new SelectList(db.environmenttypes, "EnvironmentTypeId", "EnvironmentTypeName", 1); 
      if (ModelState.IsValid) 
      { 
       IO io = new IO(); 
       if (file != null) 
       { 
        AppUpdate updateLog = io.UpdateApp(file, env.EnvironmentTypeId); 
        updateLog.UserName = User.Identity.Name; 
        updateLog.EnvironmentTypeId = env.EnvironmentTypeId; 
        db.appupdates.Add(updateLog); 
        db.SaveChanges(); 
       } 
       else 
       { 
        return RedirectToAction("Create"); 
       } 
       return RedirectToAction("Index"); 
      } 

      return View(); 
     } 

所以我增加了更多的模式來獲得詳細信息:

public ActionResult Create([Bind(Include="EnvironmentTypeId")] AppUpdate env, HttpPostedFileBase file) 
     { 
      ViewBag.EnvironmentTypeId = new SelectList(db.environmenttypes, "EnvironmentTypeId", "EnvironmentTypeName", 1); 
      if (ModelState.IsValid) 
      { 
       IO io = new IO(); 
       if (file != null) 
       { 
        AppUpdate updateLog = io.UpdateApp(file, env.EnvironmentTypeId); 
        updateLog.UserName = User.Identity.Name; 
        updateLog.EnvironmentTypeId = env.EnvironmentTypeId; 
        db.appupdates.Add(updateLog); 
        db.SaveChanges(); 
       } 
       else 
       { 
        return RedirectToAction("Create"); 
       } 
       return RedirectToAction("Index"); 
      } 

      return View(); 
     } 

爲什麼是無效的嗎?它出什麼問題了?請先讓我知道,如果我錯過了更多的細節,我沒有添加,然後再標記下來。

+0

不可能告訴你的時候不顯示模型或'ModelState'錯誤 – DavidG

+1

@DavidG如果他知道有關的ModelState錯誤,他就不會在這裏: D –

+0

@JoePhillips確實如此。雖然我打賭這是因爲需要驗證'Bind'排除屬性。 – DavidG

回答