DropDownListFor从哪里获取模型?
您可以使用创建下拉列表
@Html.DropDownListFor(model => model.SelectedId, model.DropDownItems);
,但是 DropDownListFor 从哪里获取要传递到 model => 的值? model.SelectedId lambda 来自?
You can create a dropdownlist using
@Html.DropDownListFor(model => model.SelectedId, model.DropDownItems);
but where does DropDownListFor get the value to pass into the model => model.SelectedId
lambda from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SelectedId 只是模型中字段的整数。
模型是从控制器传入的,
并且模型可以在顶部的视图中定义
,因此下拉框的值设置为模型中的
SelectedId
字段。例如,如果这样可以更好地说明的话,正确的字段名称将是
RoomNo
。只要
model.DropDownItems
(或在我的示例中的model.Rooms
)包含具有该属性值的项目,它就会将其设置为列表中的选定项目作为默认值。编辑:
如果您使用的是 viewbag,而不是模型,请改用
DropDownList
。 (每个输入都有一个用于模型的for
扩展)上面创建了一个 ID 为
RoomNo
的输入表单,并使用一个
SelectList
,它将如果为空则默认为空。定义显示的值和文本的字段设置在选择列表参数的末尾。最后一位将默认值设置为
@ViewBag.RoomNo
的内容。您还可以在控制器中创建选择列表,然后只需将下拉选项设置为
viewbag.myselectList
实体。SelectedId is just the integer of the field in the model.
the model is passed in from the controller using
And the model can be defined in the view at the top
So the dropdownbox's value is set as the
SelectedId
field in your model.A proper field name for example would be
RoomNo
if that clarifies it better.As long as
model.DropDownItems
(ormodel.Rooms
in my example) Contains a item with the value of that attribute, it will set it as the selected item in the list as default.Edit:
If you are using viewbag, rather than the model, instead use
DropDownList
. (each input has afor
extension for use with models)This above, creates an input form with id
RoomNo
,and uses a
SelectList
that will default to empty if null. The fields that defined the values and text shown are set at the end of the select list parameters.The last bit sets the default value to the contents of
@ViewBag.RoomNo
.You can also create the select list in the controller, and then simply set the dropdown options to the
viewbag.myselectList
entity.