I am trying to get the following to work
Function AddService(id As Integer) As ViewResult
Dim serv As RequestedService = New RequestedService
serv.JobId = id
Dim ServiceList = New List(Of RequestedService)()
Dim ServiceQuery = From s In db.Services
Select s
ServiceList.AddRange(ServiceQuery)
ViewBag.ServiceId = New SelectList(ServiceList, s.IDServices, s.ServiceName)
Return View(serv)
End Function
But
ViewBag.ServiceId = New SelectList(ServiceList, s.IDServices, s.ServiceName)
Gives me the following error: ‘s’ is not declared. It may be inaccessible due to its protection level.
I cannot work out how to correct this. Essentially I need to populate the SelectList with both an id and string and pass it to the view?
Assuming that the
RequestedServicemodel has properties calledIDServicesandServiceNameyou could use the following:But you really don’t need this intermediary list:
And you really, really, really don’t need to use ViewBag, but use view models instead.
and now your view is strongly typed to the view model: