asp.net-mvc 使用 ASP.NET MVC ViewBag 和 DropDownListfor 时遇到困难
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5377402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Having difficulty using an ASP.NET MVC ViewBag and DropDownListfor
提问by learning
My difficulty is how to use ViewBag
with DropdownListFor
?
我的难点是如何使用ViewBag
with DropdownListFor
?
In my controller I am having:
在我的控制器中,我有:
TestModel model = new TestModel();
ViewBag.Clients = model.Clients;
ViewBag.StatusList = model.StatusList;
ViewBag.enumStatus = model.enumStatus;
ViewBag.intClient = model.intClient;
In my TestModel
在我的测试模型中
public SelectList Clients { get; set; }
public SelectList StatusList { get; set; }
public ActiveStatus enumStatus { get; set; }
public int? intClient { get; set; }
In my View
在我看来
I want to use DropDownListFor
to display the ViewBag values, how can I do that?
我想用来DropDownListFor
显示 ViewBag 值,我该怎么做?
回答by Darin Dimitrov
You could do this:
你可以这样做:
@Html.DropDownListFor(x => x.intClient, ViewBag.Clients)
But I would recommend you to avoid ViewBag/ViewData and profit from your view model:
但我建议您避免使用 ViewBag/ViewData 并从您的视图模型中获利:
public ActionResult Index()
{
var model = new TestModel();
model.Clients = new SelectList(new[]
{
new { Value = "1", Text = "client 1" },
new { Value = "2", Text = "client 2" },
new { Value = "3", Text = "client 3" },
}, "Value", "Text");
model.intClient = 2;
return View(model);
}
and in the view:
并在视图中:
@Html.DropDownListFor(x => x.intClient, Model.Clients)
回答by DavidAndroidDev
Personally...I create a List and do this.
就个人而言……我创建了一个列表并执行此操作。
public ActionResult SomeAction()
{
var list = new List<SelectListItem>();
list.Add(new SelectListItem(){Text = "One", Value="One"});
list.Add(new SelectListItem(){Text = "Two", Value="Two"});
list.Add(new SelectListItem(){Text = "Three", Value="Three"});
list.Add(new SelectListItem(){Text = "Four", Value="Four"});
ViewBag.Clients = list;
return View();
}
and then in your view...
然后在你看来...
@Html.DropDownListFor(x => x.SomePropertyOnModel, (IEnumerable<SelectListItem>)ViewBag.Clients);
Notice the cast on the Viewbag item. The cast is required because the viewbag has no idea what the object is for Viewbag.Client
. So the cast there is required.
注意 Viewbag 项目上的演员表。强制转换是必需的,因为查看包不知道对象是做什么的Viewbag.Client
。所以需要演员阵容。
回答by Martin
@Html.DropDownListFor(x => x.intClient, new SelectList(Model.Clients, "ClientId", "ClientName"), string.Empty);
The ClientId is the value of the dropdown option.
ClientId 是下拉选项的值。
The ClientName is the text of the dropdown option.
ClientName 是下拉选项的文本。
The string.Empty
at the end adds a blank entry to the dropdown.
最后string.Empty
在下拉列表中添加了一个空白条目。
回答by Campinho
Here you can find a good reference: http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx
在这里你可以找到一个很好的参考:http: //blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx