我想在 ASP.NET MVC 中将 a 中的静态项目列表分配给 a SelectList()
,Html.DropDownList()
最佳做法是什么?
我正要尝试找到一种使用方法,new SelectList(new {key = "value"}...
但是一个不起作用,第二个,我会在这里违反法律吗,我的静态列表是否应该以任何方式声明ViewData
并传递为IList/IENumerable
?
我想在 ASP.NET MVC 中将 a 中的静态项目列表分配给 a SelectList()
,Html.DropDownList()
最佳做法是什么?
我正要尝试找到一种使用方法,new SelectList(new {key = "value"}...
但是一个不起作用,第二个,我会在这里违反法律吗,我的静态列表是否应该以任何方式声明ViewData
并传递为IList/IENumerable
?
最好不要在视图中创建 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 对象、值字段、文本字段和选定值。
在视图中:
<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>
所有 MVC 新手最初都避免使用“M”字,但它确实从 MVC 首字母缩写词的开头开始。所以也许,只是也许,你可能想用一个模型开始你的解决方案......只是说。
不要重复自己(干)。您将最终将“new PageData()”复制并粘贴到将选项列表传递给视图的每个控制器。然后,您将要删除或添加一个选项,并且必须编辑每个控制器的 PageData。
此外,您希望以最少的不必要的冗长“add”、“new”、“IS”和“Name”键入最少的代码。因为您在选择选项(和/或单选按钮列表)中只有一个键值对,所以在您的模型中使用尽可能轻的数据结构,即 Dictionary --。
然后只需在 Controller 中引用 Model 并使用包含 LINQ Lambda 表达式的 DropDownListFor 将 Dictionary 转换为 View 中的 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);
}
视图使用 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() 中的“Value”中的 VALUE 感到困惑,它是字典中的键,它是在选项标签之间结束的文本/标题(例如 Eggs Benedict)。
用例:为了最大限度地减少应用程序和数据库之间的流量,我创建了一个静态列表,以避免数据库查询下拉列表,如果有的话,很少更改。然而,变化是不可避免的,从现在起六个月后,我客户餐厅的一名用餐者去世了;不是因为绿色的火腿,而是因为他们对鸡蛋过敏,厨师在他们的华夫饼里混了一些。
餐厅需要立即更新他们的客户信息以包括食物过敏。虽然他们喜欢回头客,但他们对死去的顾客作为僵尸回来并不酷,因为他们的信用卡被取消了,他们无法付款。
反问:我应该修改所有与客户鸡蛋偏好相关的控制器和视图吗?或者只是将 { 7, "Allergic to Eggs"} 插入模型中?
另一个反问:煎蛋不是鸡蛋吗?是否要向模型中添加一次 {8, "Omelette, Western"}, {9, "Omelette, Mushroom-Feta-Spinach"} 并让新添加的内容自动传播到所有使用它们的视图中的所有下拉列表中?
底线这可能比您要求的要多,...但您确实说过 MVC,而不仅仅是 VC: 1. 在 * M *VC
中使用模型。2. Use a dictionary in the Model when a select list is based on nothing more than a "primary key" and a title. 3. 如果您的静态列表与某处的数据库查找表不匹配,则您的应用程序可能不是很有用。当您向静态列表添加选项时,您很可能还需要对查找表执行插入操作,以避免与数据库中的其他表发生主键/外键关系完整性错误。4. 使用 lambda 和强类型数据结构来避免错误并获得 typeahead 支持。
好的,我决定接受自己的建议,这应该在控制器中定义:
仅供参考,我刚回来:
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);
...(忽略上下文)并在 View ASPX 端:
<%= Html.DropDownList("type.field", new SelectList(ViewData.Model.Formats, "ID", "Name"...
如果有人有更优化的方法,我会很乐意接受他们的回答。