Vb.net 使用模型的下拉列表
我试图使用下面的下拉框为模型选择一系列要编辑的值 到目前为止,我已经获得了以下代码:Vb.net 使用模型的下拉列表,vb.net,asp.net-mvc-3,Vb.net,Asp.net Mvc 3,我试图使用下面的下拉框为模型选择一系列要编辑的值 到目前为止,我已经获得了以下代码: @Html.DropDownList("Services", "") 但本质上我想在这里保存该字符串,而不是: @Html.EditorFor(Function(model) model.ServiceName) 我的看法是: @Using Html.BeginForm() @Html.ValidationSummary(True) @<fieldset> <
@Html.DropDownList("Services", "")
但本质上我想在这里保存该字符串,而不是:
@Html.EditorFor(Function(model) model.ServiceName)
我的看法是:
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>RequestedService</legend>
<div class="editor-label">
@Html.LabelFor(Function(model) model.ServiceId)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.ServiceId)
@Html.DropDownList("Services", "")
@Html.ValidationMessageFor(Function(model) model.ServiceId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
End Using
最后是我的模型:
Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations
Public Class RequestedService
Public Property RequestedServiceId() As Integer
Public Property ServiceId() As Integer
<Required()>
<Display(Name:="Job Number *")>
Public Property JobId() As Integer
<Required()>
<Display(Name:="Station ID *")>
Public Property StationId() As Integer
End Class
导入System.Data.Entity
导入System.ComponentModel.DataAnnotations
公共类RequestedService
公共属性RequestedServiceId()为整数
公共属性ServiceId()为整数
公共属性JobId()为整数
公共属性StationId()为整数
末级
选择列表的问题在于,您需要告诉选择列表哪些是值,哪些是显示文本。您不能只传递字符串列表,要正确填充,请像这样添加键和值
Dim ServiceQuery = (From s In db.Services
Select s)
ViewBag.ServiceId = New SelectList(ServiceList, s.IDServices, s.ServiceName)
如果您需要与服务相关的ID,则可能是这样
ViewBag.Services = New SelectList(ServiceList, s.IDServices, s.ServiceName)
或者,如果您只需要文本作为值
ViewBag.Services = New SelectList(ServiceList, s.ServiceName, s.ServiceName)
更新
要实现这一点,您需要修改视图和操作
首先,在操作中更改Viewbag元素的名称,如下所示
Dim ServiceQuery = (From s In db.Services
Select s)
ViewBag.ServiceId = New SelectList(ServiceList, s.IDServices, s.ServiceName)
现在,你观点的明显变化是
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>RequestedService</legend>
<div class="editor-label">
@Html.LabelFor(Function(model) model.ServiceId)
</div>
<div class="editor-field">
@Html.DropDownList("ServiceId", "")
@Html.ValidationMessageFor(Function(model) model.ServiceId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
当用户从下拉列表中选择选项并单击“创建”按钮时,属性ServiceID将自动映射到您的类中,即mvc3与元素的名称一起工作,为您完成所有神奇的工作您需要告诉seleclist的
选择列表
的问题,哪些是值,哪些是显示文本。您不能只传递字符串列表,要正确填充,请像这样添加键和值
Dim ServiceQuery = (From s In db.Services
Select s)
ViewBag.ServiceId = New SelectList(ServiceList, s.IDServices, s.ServiceName)
如果您需要与服务相关的ID,则可能是这样
ViewBag.Services = New SelectList(ServiceList, s.IDServices, s.ServiceName)
或者,如果您只需要文本作为值
ViewBag.Services = New SelectList(ServiceList, s.ServiceName, s.ServiceName)
更新
要实现这一点,您需要修改视图和操作
首先,在操作中更改Viewbag元素的名称,如下所示
Dim ServiceQuery = (From s In db.Services
Select s)
ViewBag.ServiceId = New SelectList(ServiceList, s.IDServices, s.ServiceName)
现在,你观点的明显变化是
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>RequestedService</legend>
<div class="editor-label">
@Html.LabelFor(Function(model) model.ServiceId)
</div>
<div class="editor-field">
@Html.DropDownList("ServiceId", "")
@Html.ValidationMessageFor(Function(model) model.ServiceId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
当用户选择dropdownlist中的选项并单击create按钮时,属性ServiceID将自动映射到您的类中,这是mvc3与元素的名称一起工作,为您完成所有神奇的工作您可以向我们展示您的行动和模型吗?您可以向我们展示您的行动和模型吗?这非常有意义!谢谢我知道我现在已经创建了一个下拉框,但是我如何告诉我的视图它需要选择id并将其作为@Html.EditorFor(Function(model)model.ServiceId)放置在model.ServiceId中呢?这很有意义!谢谢我知道我现在已经创建了一个下拉框,但是我如何告诉我的视图它需要获取所选的id并将其作为@Html.EditorFor(Function(model)model.ServiceId)放置在model.ServiceId中呢?