在dropdownlistfor MVC 4选择默认值 [英] Select a default value in dropdownlistfor MVC 4

查看:219
本文介绍了在dropdownlistfor MVC 4选择默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与一个选定值dropdownlistfor,但它不工作:/我在网上搜索上,但我没有找到解决办法:/

I'm trying to make a dropdownlistfor with a selected value but it doesn't work :/ And I search on the web but I don't find the solution :/

有关的那一刻,我这样做是:

For the moment, I'm doing this :

在C#中:

ViewBag.ID_VEH = new SelectList(db.VEHI, "ID_VEH", "COD_VEH", 4); // 4 is an example

在我CSHTML:

@Html.DropDownListFor(model => model.ID_VEH, ViewBag.ID_VEH as SelectList)

将DropDownList很好完成,但默认值未选择:?/你有一个想法,请

The dropdownlist is well complete but the default value is not selected :/ do you have an idea please ?

推荐答案

我喜欢做的就是添加的项目在DropDownList我的模型显示的列表,所以我没有通过通过该列表viewbag。
此外,我喜欢一个字段添加到我的模型包含的SelectedValue,我填写控制器

What I like to do is add a list of the items to display in the dropdownlist to my model, so I don't have to pass that list via a viewbag. Also i like to add a field to my model that contains the SelectedValue, that I fill in the controller

然后,你可以做

@Html.DropDownListFor(model => model.ID_VEH, new SelectList(Model.listVEH, "ID_VEH", "COD_VEH", Model.SelectedVEH_ID), "--Select VEH--")

这篇关于在dropdownlistfor MVC 4选择默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆