创建一个DropDownList [英] Creating a DropDownList

查看:131
本文介绍了创建一个DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些列出许多结果的表的页面。我想向大家介绍多的DropDownList来筛选结果。

I have a page with a table which list many results. I would like to introduce multiple dropdownlist to filter the results.

我想知道,如果它可以更好地打造从后面code SelectedItems的列表,并通过它通过视图模型或直接在剃刀页面上创建DropDownList的?

I would like to know if it could be better to create the List of SelectedItems from code behind and pass it through the viewModel or create the DropDownList directly on the Razor page?

DropDownList控件永远不会改变此页面上。

The DropDownList will never change on this page.

推荐答案

请注意,你可以有很多解决方案,但它是一个令人关注的控制器内分离到code更好。所以,你可以把你的列表(下拉)在 ViewBag 对象,然后在查看刚刚创建这个下拉 ViewBag 作为的SelectList。对于更多的灵活性和尊重MVC模式的视图只是用来显示控制器中的数据。

Note that You can have many solutions but it is better for the separation of concern to code inside the controller. So you can put your list (for the dropdown) in a ViewBag object and then in the View you just create a dropdown with this ViewBag as SelectList. For more flexibility and respect of MVC pattern the view is just used to display data from the controller.

下面只是一个例子:

在您的控制器:

public ActionResult Index()
{
 ....
 ViewBag.MyDropDownList1 = context.someTables.Select(t=>t);
}

而在你查看:

@Html.DropDownListFor(m=>m.Somefields, ViewBag.MyDropDownList1 as SelectList,"Make the choice" )

这篇关于创建一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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