I've been searching for a solution for about a month, however, nobody is using the functions the same way my software (written by a previous programmer) is using the functions. I've tried every solution provided; without exception, an error always occurs of one form or another. So this is the original coding. I'm tring to create a new row in a SQL Server table. On the screen where I'm inputting the data, the optional field Gender is before the require field Pay Grade. Both are drop down lists. If i skip the Gender and pick a Pay Grade, the line in the View that fails is the dropdownlist for the Gender. the Pay Grade is only required because we want it to be.
I'm only including the coding where the failure is occuring. I'm using Visual Studio 2019; this error started when we upgraded to VS 2017 last year. Also using HTML, Razor, javascript, and Visual Basic. This is the error: InvalidOperationException: The ViewData item that has the key 'GenderID' is of type 'System.Int32' but must be of type 'IEnumerable'.
What am I missing?
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 Partial Public Class Member
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
End Class
View
@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>