【发布时间】:2011-02-23 23:56:05
【问题描述】:
我是 MVC3 的新手,一直在使用 EF 和“代码优先”在一个小型站点上工作。我正在尝试在处理下拉列表的视图中做一些事情,并且想知道处理它们的最佳方法是什么。我希望用户能够从下拉列表中选择规则,并且根据选择的规则,我希望页面上的标签显示规则名称(不发布)。我还需要能够将所选规则发送到下一页。我还没有将所有必要的字段添加到视图中,因为我真的不知道它应该如何工作。我应该如何尝试这样做?
我有我的模型:
public class D1K2N3CARule
{
public int ID { get; set; }
[Required]
public int Name { get; set; }
[Required]
public string Rule { get; set; }
public D1K2N3CARule(int name, string rule)
{
Name = name;
Rule = rule;
}
public D1K2N3CARule()
{
Name = 0;
Rule = "";
}
}
我的视图模型:
public class D1K2N3CARuleViewModel
{
public string SelectedD1K2N3CARuleId { get; set; }
public IEnumerable<D1K2N3CARule> D1K2N3CARules { get; set; }
}
我的控制器:
public ActionResult Index()
{
var model = new D1K2N3CARuleViewModel
{
D1K2N3CARules = db.D1K2N3DARules
};
return View(model);
}
和我的观点:
'@model CellularAutomata.Models.D1K2N3CARuleViewModel
@{
ViewBag.Title = "Index";
}
<asp:Content id="head" contentplaceholderid="head" runat="server">
<script type="text/javascript">
</script>
</asp:Content>
<h2>Index</h2>
<table>
<tr>
<td>
@Html.DropDownListFor(
x => x.D1K2N3CARules,
new SelectList(Model.D1K2N3CARules, "ID","Rule")
)
</td>
</tr>
</table>'
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 drop-down-menu html.dropdownlistfor