首頁 > 後端開發 > C++ > 為什麼我的下拉列表拋出' system.int32”與' iEnumerable”錯誤?

為什麼我的下拉列表拋出' system.int32”與' iEnumerable”錯誤?

Susan Sarandon
發布: 2025-02-03 03:06:10
原創
647 人瀏覽過

Why Does My Dropdown List Throw a

>故障排除“ system.int32” vs.“ iEnumerable”下拉列表中的錯誤 >提交帶有下拉列表的表單有時會導致錯誤指示類型不匹配的錯誤:

>項目期望

> type(用於下拉列表選項),但會接收ViewData>。 IEnumerable System.Int32根本原因:null類別列表

> >這通常是因為視圖模型中的屬性(例如,

)是

>時。 CategoryList依靠此屬性填充下拉列表。 ProjectVM> nullViewData解決方案:在“郵政方法”中重新填充類別列表

> 解決方案是在返回視圖之前明確地重新填充控制器的post Action方法之前的

屬性。 這樣可以確保在

>中可用正確的數據。 CategoryList這是修改您的帖子方法的方法: IEnumerable添加此行重新創建ViewData並將其分配給

。 反過來,這為您的視圖中的幫助者提供了必要的

>數據。

<code class="language-csharp">public ActionResult Create(ProjectVM model)
{
    if (!ModelState.IsValid)
    {
        // Repopulate the CategoryList
        model.CategoryList = new SelectList(db.Categories, "ID", "Name"); 
        return View(model);
    }
    // ... (Your existing code to save the model and redirect) ...
}</code>
登入後複製
的工作方式:ViewData和SelectList

SelectList> model.CategoryListIEnumerable helper方法(和DropDownListFor())試圖找到適當的

數據。如果它沒有直接接收

,則使用屬性名稱(例如'categoryId')在中查找數據。如果項目不可鑄入

,則會發生不匹配錯誤。 通過在郵政方法中明確設置

,我們繞過了此問題。

以上是為什麼我的下拉列表拋出' system.int32”與' iEnumerable”錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板