- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设给定一个 html 表格的分部 View ,每个表格行都存在一个嵌套的分部 View ,如下所示:
外部局部 View
<table>
<thead>
<tr>
<th>Item Name</th>
<th>Cost</th>
<th>Category</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.PurchaseItems.Count; i++)
{
@Html.Partial("_PurchaseItemsDetail", Model.PurchaseItems[i])
}
</tbody>
</table>
内部局部 View ,注意:外部 View 可以在单个请求中多次使用此局部 View ,但是如果使用 AJAX 请求,它也可以只使用一次
<tr id="@Model.ID">
<td>@Html.TextBoxFor(x => x.ItemName)</td>
<td>@Html.TextBoxFor(x => x.Cost)</td>
<td>
@Html.DropDownListFor(x => x.Category, CATEGORY_COLLECTION)
</td>
</tr>
问题:给定这个旨在灵活的内部 View ,查询数据库和临时存储 IEnumerable<SelectListItem>
的最佳方式是什么?与下拉列表一起使用的集合?我能想到的方法:
方法一:这似乎是最简单的,但感觉违反了MVC原则。在内部局部 View 中计算并以仅命中一次数据库的方式存储在 ViewData 集合中:
@{
if (ViewData["Categories"] == null)
{
ViewData["Categories"] = API.GetCategories();
}
}
然后在后面的 View 中
<td>
@Html.DropDownListFor(x => x.Category, ViewData["Categories"])
</td>
这很好,因为 View 会自行决定如何填充其包含的下拉列表。它不依赖于 View 模型。
方法 2:同上,除了在返回内部局部 View 的各种 Controller 方法中设置 ViewData。这种方式似乎更符合 MVC 最佳实践,但考虑到每个 Controller 方法都需要正确设置 ViewData 以及创建必要的 View 模型,因此维护起来似乎更加乏味和困惑。
方法 3:这看起来最费功夫,最难维护,但最符合 MVC 原则。避免完全使用 ViewData,而是将对集合的引用与分部 View 模型中的其他属性一起存储。 Controller 负责创建集合,然后在每个表行的 View 模型中存储一个引用。
public class PurchaseItemModel
{
public string ItemName { get; set; }
public decimal Cost { get; set; }
public string Category { get; set; }
// add this
public IEnumberable<SelectListItem> Categories { get; set; }
}
并且在为内部 View 提供服务的每个 Controller 中
// if dealing with multiple rows (GET)
IEnumerable<SelectListItem> collection = API.GetPurchaseItemList();
foreach (PurchaseItemModel pim in OuterModel.PurchaseItemModels)
{
pim.Categories = collection;
}
或
// if dealing with a single row (Ajax)
purchaseItem.Categories = API.GetPurchaseItemList();
然后在后面的 View 中
<td>
@Html.DropDownListFor(x => x.Category, x.Categories)
</td>
也许这只是主观问题,但似乎有针对此类情况的最佳实践。有什么想法吗?
最佳答案
继续创建一个 ViewModel 类,例如
public class PurchaseItemViewModel
{
public string ItemName { get; set; }
public decimal Cost { get; set; }
public string Category { get; set; }
public IEnumberable<Category> Categories { get; set; }
public PurchaseItemViewModel(PurchaseItemModel item, List<Category> categories)
{
//initialize item and categories
}
}
在 Controller 中获取所有项目,然后获取所有类别,然后将购买项目设置到 ViewModel 并设置类别。因此,您会将 ViewModel 类传递给 View 。
关于c# - 如何将 IEnumerable<SelectListItem> 集合传递给 MVC 局部 View ,每个请求仅进行一次数据库查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19735372/
我正在尝试为用户填写一个表单以注册到 asp.net MVC 网站,我正在努力让下拉列表正确显示文本。该模型具有以下内容: public SelectList CountiesList; 在构造函数中
我正在将现有的 MVC3 应用程序从 InProc 移动到 ASP.Net session 状态服务。其中一个模型对象具有此属性: public List StateCodes { get; set;
我有一个带有常量的静态类,我想将其存储在一个列表中,以便在 DropDown 中使用 public static class DateFormatConstant { [Display(Nam
我正在尝试从 selectlistitem 中删除一个项目。到目前为止,我还没有取得任何成功 代码截图如下 使用 System.Collections.Generic; List ab
问题 我目前正在向我的 MVC 项目添加自动映射,但我遇到了困难。现在我有一个用户模型用于表示数据库中的数据。我必须将该模型映射到将在调用 Edit 方法时使用的 EditUserModel。 Edi
我正在尝试从数据库中填充下拉列表, 我正在关注这个 link 我在模型中写了如下代码 public class FillDeptName { public IList Drp_Name { g
我有以下类(class): public static IEnumerable GetDatastore() { return new[] { new
我有以下代码,但我有错误 Error 6 foreach statement cannot operate on variables of type 'int' because 'int' does
我有一个这样的对象: public class AppointmentStatus { public int Id {get;set;} public string I18NKey
我在从 SelectListItem 获取选定值时遇到问题。 这是我的模型: public class GiveCourseNameModel { public string Selected
忙于为 Controller 编写单元测试,该 Controller 生成一个 View 模型,其中包含一个选项列表,如 IEnumerable 。我尝试检查预期列表是否包含 View 模型中的所有列
我正在尝试将一个项目添加到 IEnumerable SelectList。我有一个填充我的列表的初始查询,然后我有一个查询来检查是否存在名为“INFORMATIONAL”的项目。如果没有,我需要将它添
我正在尝试为下拉列表获取不同的值,但是当使用以下语法时,它会返回所有行。 有人可以告诉我如何使用正确的语法获取不同的值集吗? IEnumerable ldidList = _db.TrafficHit
这里有一个棘手的问题。我正在尝试将列表项转换为 IEnumerable . 列表 dynamicTextInDatabase只需返回我的数据库中使用的所有枚举。目前只返回 1 和 2。数据类 simp
我有一个 asp.net-mvc 网页,我想显示一个基于枚举的下拉列表。我想显示每个枚举项的文本和 id 是与枚举关联的 int 值。有什么优雅的方法可以进行这种转换吗? 最佳答案 您可以使用 LIN
无论如何,Automapper 是否可以忽略某种类型的所有属性?我们正试图通过验证 Automapper 映射来提高我们代码的质量,但不得不输入 .Ignore()所有 IEnumerable总是手动
public IEnumerable GetList(int? ID) { return from s in db.List orderby s.Descrip
这个问题与 ASP.NET MVC 2 RC(12 月下降)有关。基本问题是,当创建具有默认选定值的下拉列表时,“selected”属性要么未呈现为 HTML,要么归因于错误的选项。 下面是一些演示错
retval.AddRange(oldList.Union(newList)); oldList 和newList 都在这里声明 List oldList = new List(); List new
我在这里拉扯我的头发。我正在使用 DropDownListFor MVC3 中带有枚举的 HTML 帮助器,如下所示: @Html.DropDownListFor(model => model.Tit
我是一名优秀的程序员,十分优秀!