如何添加项目的静态列表中的MVC Html.DropDownList()
-
22-08-2019 - |
题
我想在SelectList()
在ASP.NET MVC一个Html.DropDownList()
分配项目的静态列表,什么是最好的做法是什么?
我正要试图找到一种方法,使用new SelectList(new {key = "value"}...
但一个,没有工作,两个,我会在这里打破了法律,应我的静态列表可以在ViewData
反正声明和IList/IENumerable
?通过
解决方案 3
OK,我决定把我自己的意见,这应该在控制器中定义的:
仅供参考,我刚回到:
PageData data = new PageData()
{
Formats = new[]
{
new { ID = "string", Name = "Text" },
new { ID = "int", Name = "Numeric" },
new { ID = "decimal", Name = "Decimal" },
new { ID = "datetime", Name = "Date/Time" },
new { ID = "timespan", Name = "Stopwatch" }
},
.............
};
return View(data);
...(忽略上下文)和在视图ASPX侧:
<%= Html.DropDownList("type.field", new SelectList(ViewData.Model.Formats, "ID", "Name"...
如果任何人有这样做的更好的方法,我会很乐意接受他们的回答。
其他提示
这是不创建在视图中的SelectList的最佳实践。你应该在控制器创建并使用的ViewData传递它。
示例:
var list = new SelectList(new []
{
new {ID="1",Name="name1"},
new{ID="2",Name="name2"},
new{ID="3",Name="name3"},
},
"ID","Name",1);
ViewData["list"]=list;
return View();
传递到构造:了IEnumerable对象,值字段中的文本字段和所选择的值
。在View:
<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>
所有MVC菜鸟最初避免“M”字,但它确实有点在MVC缩写的起点开始。因此,也许,只是也许,你可能要开始与模型解决方案......只是说。
不要重复自己(DRY)。你会最终复制和粘贴“新PageData()”,以每每一个通过选项列表的视图控制器。然后,你将需要删除或添加的选项,并有编辑每个控制器的PageData。
此外,要具有最少不必要地详细的“添加” S“新的” s到类型的代码量最少的,“IS”,且“名” S。因为你只需要在选择选项键 - 值对(和/或单选按钮列表)中,使用最轻数据结构可能的,即,字典--in模型。
然后,只需引用在控制器模型和使用含有LINQ Lambda表达式一个DropDownListFor词典转换成的SelectList在视图中。
慑于?你不是一个人。我当然是。下面是我用于自学的M时,V,以及C的示例:
MVC的模型部分:
using System.Web.Security;
using System.Collections.Generic;
using System.Text;
using System.Linq.Expressions;
using System.Web.Routing;
using System.Web.Helpers;
using System.Web.Mvc.Html;
using MvcHtmlHelpers;
using System.Linq;
// EzPL8.com is the company I work for, hence the namespace root.
// EzPL8 increases the brainwidths of waiters and bartenders by augmenting their "memories" with the identifies of customers by name and their food and drink preferences.
// This is pedagogical example of generating a select option display for a customer's egg preference.
namespace EzPL8.Models
{
public class MyEggs
{
public Dictionary<int, string> Egg { get; set; }
public MyEggs() //constructor
{
Egg = new Dictionary<int, string>()
{
{ 0, "No Preference"}, //show the complete egg menu to customers
{ 1, "I hate eggs"}, //Either offer an alternative to eggs or don't show eggs on a customer's personalized dynamically generated menu
//confirm with the customer if they want their eggs cooked their usual preferred way, i.e.
{ 2, "Over Easy"},
{ 3, "Sunny Side Up"},
{ 4, "Scrambled"},
{ 5, "Hard Boiled"},
{ 6, "Eggs Benedict"}
};
}
}
在控制器是现在非常简单,只是路过模型。它避免了产生的分离的概念,这可能不是孤立的只是一个页:
public ActionResult Index()
{
var model = new EzPL8.Models.MyEggs();
return View(model);
}
在View使用DropDownListFor(而不是DropDownList的),而且需要一个Lambda表达式用于在事件重构强类型:
@Html.DropDownListFor(m => m.Egg, new SelectList( Model.Egg, "Key", "Value"))
瞧,所得到的HTML:
<select id="Egg" name="Egg">
<option value="0">No Preference</option>
<option value="1">I hate eggs</option>
<option value="2">Over Easy</option>
<option value="3">Sunny Side Up</option>
<option value="4">Scrambled</option>
<option value="5">Hard Boiled</option>
<option value="6">Eggs Benedict</option>
</select>
注意:不要被在<option value="6">
的值,这是从字典密钥,从在的SelectList“值”(),它是文本/标题(例如蛋笃),该间结束混淆选项标签。
使用案例强>: 为了最大限度地减少应用程序和数据库之间的流量我创建了一个静态列表,以避免下拉很少,如果发生了改变,列表中的数据库查询。然而,变化是从现在起我的顾客的餐厅用餐模具不可避免的,六个月;不是因为绿色火腿,而是因为他们对鸡蛋过敏和库克与他们的松饼混合一些。
在餐厅需要更新,以立即将食物过敏他们的客户信息。虽然他们爱的回头客,他们没有与死者的客户回来为没有办法支付,因为他们的信用卡被取消僵尸冷却。
反问:我应该修改有关的所有客户鸡蛋喜好控制器和视图?或者简单地插入{7,“对鸡蛋过敏”}到模型?
另一个反问:是不是煎蛋鸡蛋?是否要添加{8,“煎蛋,西方的”},{9,“煎蛋,蘑菇,羊乳酪菠菜”}一旦到模型上,有新的增加自动地在所有使用它们的意见在所有下拉列表中传播?
底线(S),这大概比你要的,......但你没有说MVC,不仅仅是VC方式更多:点击 1. * 中号 * VC使用模型。 2.使用在模型中的字典时选择列表是基于无非是“主键”和标题等等。 3.如果您的静态列表不与数据库查找表JIVE地方,你的应用可能不是非常有用的。当您添加一个选项,以静态列表,更可能你还需要执行插入查找表,以避免主键/外键关系的完整性与数据库中的其他表的错误。 4.使用Lambda和强类型数据结构以避免错误,并获得预输入的支持。