Asp.net mvc 如何在编辑页面MVC中设置dropdownlist选定值

Asp.net mvc 如何在编辑页面MVC中设置dropdownlist选定值,asp.net-mvc,vb.net,drop-down-menu,html.dropdownlistfor,Asp.net Mvc,Vb.net,Drop Down Menu,Html.dropdownlistfor,如何在编辑页面中设置DropdownList所选项目?。 我应该在DropdownList中编写什么代码而不是“Nothing” 这是控制器: ' GET: MachinInfo/Edit/5 Function MachinInfoEdit(ByVal id As Integer?) As ActionResult If IsNothing(id) Then Return New HttpStatusCodeResult(HttpStatusCode.BadReq

如何在编辑页面中设置DropdownList所选项目?。 我应该在DropdownList中编写什么代码而不是“Nothing”

这是控制器:

' GET: MachinInfo/Edit/5
Function MachinInfoEdit(ByVal id As Integer?) As ActionResult
    If IsNothing(id) Then
        Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
    End If
    Dim machinInfo As MachinInfo = db.MachinInfo.Find(id)
    If IsNothing(machinInfo) Then
        Return HttpNotFound()
    End If
    ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")

    Return View(machinInfo)
End Function

' POST: MachinInfo/Edit/5
<HttpPost()>
<ValidateAntiForgeryToken()>
Function MachinInfoEdit(<Bind(Include:="Id,PelakStateID,MachinStateID,OrgCode,MachineID,BrandID,MachintypeID,NPlate,NPlateL,NPlateM,NPlateR,CardSerial,VIN,Myear,Color,GearTypeID,MCost,Mcamp,Description,LogI,LogE")> ByVal machinInfo As MachinInfo) As ActionResult
    If ModelState.IsValid Then

        db.Entry(machinInfo).State = EntityState.Modified
        db.SaveChanges()
        Return RedirectToAction("Index")
    End If
    ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")

    Return View(machinInfo)
End Function
获取:MachinInfo/Edit/5 函数MachinInfoEdit(ByVal id为整数?)作为ActionResult 如果没有(id),那么 返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest) 如果结束 将machinInfo设置为machinInfo=db.machinInfo.Find(id) 如果没有(机器信息),则 返回HttpNotFound() 如果结束 ViewBag.BrandID=新选择列表(db.Brand,“Id”,“BrandName”) 返回视图(machinInfo) 端函数 '发布:MachinInfo/Edit/5 函数MachinInfoEdit(ByVal machinInfo作为machinInfo)作为ActionResult 如果ModelState.IsValid,则 db.Entry(machinInfo).State=EntityState.Modified db.SaveChanges() 返回重定向操作(“索引”) 如果结束 ViewBag.BrandID=新选择列表(db.Brand,“Id”,“BrandName”) 返回视图(machinInfo) 端函数 和我的编辑视图:


@LabelFor(Function(model)model.BrandID,htmlAttributes:=New,带有{.class=“control label col-md-2”})
@DropDownList(“BrandID”,Nothing,“-select-”,htmlAttributes:=带有{.class=“form control”}的New)
@Html.ValidationMessageFor(Function(model)model.BrandID,“,新增为{.class=“text danger”})
您可以使用并传递选定值作为最后一个参数:

ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName", id.ToString())
ViewBag.DefaultBrand = id.ToString()
然后使用
DirectCast
TryCast
将ViewBag内容传递到
DropDownList
帮助程序:

@Html.DropDownList("BrandID", TryCast(ViewBag.BrandID, SelectList), ViewBag.DefaultBrand, htmlAttributes := New With { .class = "form-control"})
旁注:

最好创建一个viewmodel属性,该属性具有
SelectList
IEnumerable(属于SelectListItem)
类型-

Public Class ViewModel
    ' other properties

    Public Property SelectedBrandID As Integer
    Public Property BrandList As IEnumerable(Of SelectListItem)
End Class
然后在控制器操作中填充它:

Dim model As New ViewModel
model.BrandList = db.Brand.[Select](Function(x) New SelectListItem() With {
                          .Text = x.Id, 
                          .Value = x.BrandName,
                          .Selected = If(x.Id = id, True, False) }))

Return View(model)
最后使用
DropDownListFor
helper显示选项列表:

@Html.DropDownListFor(Function(model) model.BrandID, Model.BrandList, Nothing, htmlAttributes := New With { .class = "form-control"})
相关问题:


谢谢您的回复,我执行了第一个报价,但问题没有解决。加载页面时,它仍然显示“-select-”。请尝试删除
“-select-”
部分,并使用
Nothing
对其进行更改-然后应通过
SelectList
中的赋值来更改占位符。我将其替换为
Nothing
,但这次它显示在第一个列表选项上,这是不正确的。我附上了调试模式的屏幕截图,请看,有必要为
SelectList
的第四个参数指定一个值,该值存在于
DataValueField
中,以标记默认的选定项-您可以获得传递的ID,并使用
ToString()
进行设置。添加
ID.ToString
,传递
id
的问题已修复。但是
DropDownList
仍然显示在第一个列表项上,这是不正确的-我添加了第二个屏幕截图