【发布时间】:2011-04-06 16:07:35
【问题描述】:
我有一个包含许多记录的视图。每条记录都有一个相应的表单来编辑其详细信息,该表单在用户按下 Edit 之前是隐藏的,此时会显示该表单并可以使用 Ajax 发布以保存更改。
每条记录都有一个类别(另一个表的外键)。类别列表取自视图模型,它是一个 SelectList。因此,一个 SelectList of Categories 提供了许多表单(每条记录一个)
基本上,在我的记录列表中呈现的循环中,我希望编辑表单选择正确的类别。尽管 Html.DropDownList 似乎没有让您设置所选项目的重载 - 这必须在创建 SelectList 时完成 - 我只在我的 ViewModel 中这样做一次。
我错过了一个技巧吗? 我的代码:
<div class="form-row">
<label for="Category">
File Category</label>
<%= Html.DropDownList("CategoryID", Model.Categories, new { style="font-size:11px; width:150px;" })%>
</div>
此时我想根据记录的值传入选中的项
谢谢,
编辑 包含更多代码,以便更有意义: 向视图提供数据的 ViewModel 部分:
public class CustomerFilesViewModel
{
public List<CustomerFile> Files { get; set; }
//added for the manage files area of the site
public SelectList Organisations { get; set; }
public int PDFS { get; set; }
public int DOCS { get; set; }
public int JPGS { get; set; }
public double Quota { get; set; }
public double TotalFiles { get; set; }
public int SpreadSheets { get; set; }
public SelectList Categories { get; set; }
...............
在我看来,然后我会遍历文件并为每个文件创建一个编辑表单。我使用 ViewModel 中的 SelectList 填充类别 DropDownnn。我希望能够使用file.CategoryID 在循环中设置所选项目。
【问题讨论】:
标签: asp.net asp.net-mvc