2017-01-22 50 views
0

我從View獲取以下Exception沒有類型爲「IEnumerable」的ViewData項目具有密鑰

「System.InvalidOperationException」類型發生在 System.Web.Mvc.dll程序,但在用戶代碼中沒有處理的例外

附加信息:有 類型的無ViewData的項目「的IEnumerable」有'Profile_Id'這個關鍵字。

從查看

@Html.DropDownList("Profile_Id", String.Empty) 

控制器

public ViewResult Index(string name, string Profile_Id, int? page) 
{ 

    ... 
    ViewBag.Profile_Id = new SelectList(db.PROFILE.Where(y => y.ID== myId), "Profile_Id", "Name"); 

    return View(prof.ToPagedList(pageNumber, pageSize)); 
} 

回答

2

我想,你沒有得到這個錯誤第一次遇到這種觀點從GET /索引動作渲染,但而是將相應的表單提交給呈現相同視圖的POST操作。

要修正這個錯誤確保您的POST操作裏面還填充Profile_Id屬性就像你在GET動作確實,如果你打算呈現了同樣的觀點,因爲它依賴於它:

[HttpPost] 
public ActionResult Index(MyViewModel model) 
{ 
    ... process the data 

    // populate the Profile_Id property because we are 
    // redisplaying the same view which depends on it in order 
    // to show the corresponding dropdown 
    ViewBag.Profile_Id = new SelectList(db.PROFILE.Where(y => y.ID== myId), "Profile_Id", "Name"); 

    return View(model); 
} 
+0

讓我試試這個。無論如何,你有一個驚人的個人資料。 – Illep

相關問題