I have a drop down list written in Razor for a MVC app I am working on as:
@Html.DropDownList("BillId", "")
However the user does not have to select anything according to the logic of my program (the list is populated with ‘Bill’ objects in my controller). If they do not select any thing I get an error
The ViewData item that has the key 'BillId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
How do I write a statement in Razor to return a BillId of 0 if nothing is selected?
I am not sure of the syntax as I have a background in straight java and VB but something alongs the line of
If DropdownBox.SelectedIndex = 0
Else
BillId = DropdownBox.SelectedIndex
End
Controller 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.BillIdList = New SelectList(BillList, "BillId", "BillDate")
ViewBag.BillId = New SelectList(BillList, "BillId", "BillDate")
Return View(job)
End Function
The POST function for create is as below:
<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
EDIT
I was wondering too how this is passed back as in the POST I may be able to check for a ‘null’? However I feel the problem may be the moment the submit button is pressed
In your POST controller action you forgot to populate the ViewCrap (oops, I meant ViewBag) before returning the view:
But I would hyper strongly recommend you to use view models and forget about the existence of the …… (the word that I don’t want to pronounce).
UPDATE:
Now let’s look at the correct way to implement this (which is by using view models). A view model is a class that you should define for each of your views and which will represent its specific requirements. So from what you have said in the comments section to my answer you want to have a dropdown list in your view where the user has to select a bill from a dropdown and which is required.
So let’s roll the view model:
then we define our controller with the 2 actions:
and finally you will have a corresponding view which will be strongly typed to the view model:
And now, as promised in the comments section let me show what I dubbed the absolute pornographic approach to solve this and which if you implemented in your application I will have to ask you to no longer come back and ask any ASP.NET MVC related question on StackOverflow 🙂
The pornographic approach consisted into manually inserting an item with id = 0 and text = empty string into the beginning of the list and then inside the controller verifying if the selected id equals 0 in order to check whether the model is valid or not:
So in your GET action:
and inside the view: