0

我需要为 DropDown 列表设置一个默认值,如下所示:

@Html.DropDownList("BillId", "") 

用户不一定需要选择某些内容,但列表会引发错误

The ViewData item that has the key 'BillId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

如果未选择某个值并且该框处于默认空白状态。

我的控制器如下:

    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
4

1 回答 1

0

If your model Job doesn't have the property of BillId than in the view the dropdown markup should be

@Html.DropDownList("Job_BillId", ViewBag.BillId)

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

ViewBag.BillIdList =  New SelectList(BillList, "BillId", "BillDate")

And in view use this as below

@Html.DropDownListFor(model => model.BillId, ViewBag.BillIdList)

Hope this will help you.

于 2012-08-16T17:15:34.547 回答