Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 下拉列表的默认值_Vb.net_Asp.net Mvc 3 - Fatal编程技术网

Vb.net 下拉列表的默认值

Vb.net 下拉列表的默认值,vb.net,asp.net-mvc-3,Vb.net,Asp.net Mvc 3,我需要为下拉列表设置一个默认值,如下所示: @Html.DropDownList("BillId", "") 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()

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

@Html.DropDownList("BillId", "") 
    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
用户不一定需要选择某些内容,但列表会抛出一个错误

The ViewData item that has the key 'BillId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'
具有键“BillId”的ViewData项的类型为“System.Int32”,但必须为“IEnumerable”类型
如果未选择某个值,则该框保持默认空白状态

我的控制器如下所示:

@Html.DropDownList("BillId", "") 
    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
函数创建(id为整数)作为ViewResult
ViewBag.id=id
将作业变暗为作业=新作业
job.CustomerId=id
job.JobAmount=0
job.JobDate=Date.Now()
job.JobStatus=“活动”
Dim BillList=新列表(票据)()
Dim BillQuery=从s到db.票据
选择s
BillList.AddRange(BillQuery)
ViewBag.BillId=新建选择列表(BillList、“BillId”、“BillDate”)
返回视图(作业)
端函数
'
'发布:/Job/Create
函数创建(作业作为作业)作为操作结果
如果ModelState.IsValid,则
db.Jobs.Add(作业)
db.SaveChanges()
Dim customer As customer=db.Customers.Find(job.CustomerId)
Dim customerNumber作为字符串=customer.customphone.ToString()
Dim messageSender As SendMessage=新建SendMessage
Dim smsMessage As String=“洗衣房:工作编号”&Job.JobId&“已预订。当其中的个别服务准备好收取时,您将收到通知。”
messageSender.SendMessage(smsMessage,customerNumber)
Dim url为String=“/RequestedService/AddService/”+job.JobId.ToString()
返回重定向(url)
如果结束
返回视图(作业)
端函数

如果模型作业没有视图中的BillId属性,则下拉标记应为

@Html.DropDownList("Job_BillId", ViewBag.BillId)
模型作业包含属性BillId,而不是创建具有不同名称的ViewBag,并在视图标记中相应地使用它们,例如:

在控制器中创建具有不同名称而不是BillId的ViewBag

ViewBag.BillIdList =  New SelectList(BillList, "BillId", "BillDate")
鉴于此,请按如下方式使用

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

希望这对你有帮助

我的模型作业确实有BillId,所以我需要给它一个默认值?