I need to set a default value for the DropDown list as follows:
@Html.DropDownList("BillId", "")
The user doesn’t necessarily need to select something but the list throws an error
The ViewData item that has the key 'BillId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'
if a value is not selected and the box is left on it’s default blank state.
My controllers as follows:
Function Create(id As Integer) As ViewResult
ViewBag.id = id
Dim job As Job = New Job
job.CustomerId = id
job.JobAmount = 0
job.JobDate = Date.Now()
job.JobStatus = "Active"
Dim BillList = New List(Of Bill)()
Dim BillQuery = From s In db.Bills
Select s
BillList.AddRange(BillQuery)
ViewBag.BillId = New SelectList(BillList, "BillId", "BillDate")
Return View(job)
End Function
'
' POST: /Job/Create
<HttpPost()>
Function Create(job As Job) As ActionResult
If ModelState.IsValid Then
db.Jobs.Add(job)
db.SaveChanges()
Dim customer As Customer = db.Customers.Find(job.CustomerId)
Dim customerNumber As String = customer.CustCellphone.ToString()
Dim messageSender As SendMessage = New SendMessage
Dim smsMessage As String = "LAUNDRY: Job Number " & job.JobId & " has been booked in. You will be notified when individual services within it are ready for collection."
messageSender.SendMessage(smsMessage, customerNumber)
Dim url As String = "/RequestedService/AddService/" + job.JobId.ToString()
Return Redirect(url)
End If
Return View(job)
End Function
If your model Job doesn’t have the property of BillId than in the view the dropdown markup should be
And the model Job contains the property BillId than create the ViewBag with some different name and use them accordingly in view markup for example:
Create a ViewBag with different name instead of BillId in controller
And in view use this as below
Hope this will help you.