簡體   English   中英

InvalidOperationException:具有鍵“GenderID”的 ViewData 項的類型為“System.Int32”,但類型必須為“IEnumerable”<selectlistitem> '</selectlistitem>

[英]InvalidOperationException: The ViewData item that has the key 'GenderID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

我一直在尋找解決方案大約一個月,但是,沒有人像我的軟件(由以前的程序員編寫的)一樣使用這些功能。 我已經嘗試了提供的所有解決方案; 毫無例外,錯誤總是以一種或另一種形式出現。 所以這是原始編碼。 我正在嘗試在 SQL 服務器表中創建一個新行。 在我輸入數據的屏幕上,可選字段 Gender 在必填字段 Pay Grade 之前。 兩者都是下拉列表。 如果我跳過性別並選擇薪酬等級,則視圖中失敗的行是性別的下拉列表。 Pay Grade 是必需的,因為我們希望如此。

我只包括發生故障的編碼。 我正在使用 Visual Studio 2019; 當我們去年升級到 VS 2017 時,這個錯誤就開始了。 還使用 HTML、Razor、javascript 和 Visual Basic。 這是錯誤:InvalidOperationException:具有鍵“GenderID”的 ViewData 項的類型為“System.Int32”,但必須為“IEnumerable”類型。

我錯過了什么?

Controller

     <HttpGet()>
   Public Function Create() As ViewResult

         ViewBag.GenderID = PopulateTheGenderDropDownList(Nothing)

       Return View()

    End Function

    <HttpPost()>
   Public Function Create(<Bind(Exclude:="member.Coordinator,member.DisabilityType,member.EventHistories,
    member.FinalStatusID,member.Gender,member.Hospital,member.LetterHistory,member.MemberRemarks,
    member.PayGrade,member.State,member.Navigation,
    member.Members_SpecialtyClinics")> member As Member, Optional button As String = Nothing,
                    Optional enOffPayDescription As Integer = 0,
                    Optional enlistedRateRankings As Integer = 0) As ActionResult


            ViewBag.GenderID = PopulateTheGenderDropDownList(member.GenderID)

        Return View(member)
    End Function


   Private Function PopulateTheGenderDropDownList(Optional genderID As Integer? = Nothing) As SelectList

        Dim genders As DbSet(Of Gender) = db.Genders
        Return If(IsNothing(genderID), New SelectList(genders, "GenderID", "GenderCode"),
            New SelectList(genders, "GenderID", "GenderCode", genderID))

    End Function

Model 部分公眾 Class 會員

Public Property PayGradeID As Nullable(Of Integer)
Public Property GenderID As Nullable(Of Integer)

Public Overridable Property Gender As Gender
Public Overridable Property PayGrade As PayGrade

結束 Class

看法

@Using Html.BeginForm("Create", "Member", Nothing, FormMethod.Post, New With {.id = "MemberForm", .autocomplete = "off"})
 @<div class="row"> <div class="two columns"> <strong>Gender</strong> </div> <div class="one column"> @*this statement is where it fails because it can't find GenderID*@ @Html.DropDownList("GenderID", Nothing, New With {.style = "width:100px;", .class = "form-control"}) </div> <div Class="nine columns"> @Html.ValidationMessageFor(Function(model) model.Gender.GenderID) </div> </div> @<div class="row"> <div class="two columns"> <strong>PayGrade</strong> <span class="required"></span> </div> <div class="six columns"> @*this statement causes the failure when it hits the "this.form.submit()" event*@ @Html.DropDownList("EnOffPayDescription", Nothing, "Please select a PayGrade", New With {.style = "width:250px;", .onchange = "this.form.submit();", .title = "Enlisted/Officer is required which is determined by the PayGrade, so you must enter a valid PayGrade."}) </div> <div Class="four columns" id="payGradeVal"> @Html.ValidationMessageFor(Function(model) model.PayGrade.PayGradeDescription) </div> </div>

這就是缺乏 MVC 軟件經驗的地方。 當 DROPDOWNLIST 執行返回 controller 的提交時(它必須獲取一些額外的數據),其他字段之一出現錯誤; 該錯誤導致 Return View(model) 語句發生。 人們會認為這不是問題,但是,由於此視圖正在創建新記錄,因此必須重新生成所有下拉列表(幾周前我在解決方案中看到了這一點),因此我不得不更改所有內容我在 Controller 中的驗證是完全嵌套的,並跳到下拉列表構建語句,以便 Return View(model) 語句可以正確加載視圖。

我感覺這個軟件的其他部分也有同樣的問題,我會花時間找到所有這些部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM