我有一个用户必须填写的小表格,其中包含以下字段。
姓名(文字) 值(文本) 组(组 - 是从数据库表中提取的选项列表)
现在这个 View 的模型看起来像这样,
public string Name { get; set; }
public string Value { get; set; }
public int GroupID { get; set; }
现在 View 对上述模型是强类型的。
用什么方法填充下拉列表?由于数据不包含在模型中(它可能包含在模型中)我们应该使用临时/ View 数据吗? HTML 助手?实现这一目标的理想方式是什么。
最佳答案
我为此使用了一个 View 模型和一个包含所有属性的字典(我喜欢 mvc contrib 的选择框):
class LeViewModel {
public string Name { get; set; }
public string Value { get; set; }
public int GroupID { get; set; }
public Dictionary<string, string> Groups {get; set;}
}
那么在你看来你只需要做
<%: Html.Select(m => m.GroupId).Options(Model.Groups) %>
希望对您有所帮助。
关于c# - Asp.Net MVC 处理不属于模型的下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2881215/