【发布时间】:2015-10-02 13:51:25
【问题描述】:
MVC 使用我在网上找到的一个示例,我填充了一个字典并将其传递给视图。我可以查看下拉列表并选择不同的值。但我的问题是如何在代码后面调用函数,并将选择的新值发回代码后面?
//controller
toolTipsVM.ListOfMaps = GetMapIds();
public Dictionary<int, string> GetMapIds()
{
//List<int, string> mapIds = new List<int, string>();
Dictionary<int, string> mapIds = new Dictionary<int, string>();
mapIds.Add(36, "hi");
mapIds.Add(37, "how");
mapIds.Add(39, "now");
return mapIds;
}
//VW
public Dictionary<int, string> ListOfMaps { get; set; }
//View
@Html.DropDownListFor(m => m.MapId, new SelectList(Model.ListOfMaps, "Key", "Value"),
"--Choose Map--",
new {@class = "form-control"}
)
【问题讨论】:
-
MVC 中没有in code behind。你有表格吗?你想打一个ajax调用吗?你的 POST 方法是什么?
-
创建一个基于逗号的数组并通过 c# 中的 ajax 请求将它们发送到控制器。这是从视图获取数据到控制器的最简单方法。
标签: c# asp.net-mvc-3 dictionary model-view-controller ddl