【发布时间】:2011-02-20 16:46:34
【问题描述】:
我创建了一个局部视图来处理下拉列表控件。在我的模型中,我有一个成本中心列表的属性。
[UIHint("SelectionList")]
public List<SelectListItem> CostCentersList {get; private set;}
我使用了 UIHint,以便它知道我想使用我创建的 SelectionList.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<SelectListItem>>" %>
m, 模型) %>
然后,在我的主视图中,我使用以下代码来渲染控件:
<%=Html.EditorFor(m => m.CostCentersList)%>
这是工作...它呈现控件...但我在调用视图时让控件选择我想要选择的项目时遇到问题。我用一些代码做到了这一点:
BudgetDto b = Services.BudgetServices.GetBudgetById(id);
model = new BudgetViewModel
{
Amount = b.Amount,
BudgetId = id,
CostCenterId = b.CostCenterId,
Description = b.Name,
Deleted = b.Deleted
};
foreach (var i in model.CostCentersList)
if (i.Value == model.CostCenterId.ToString())
i.Selected = true;
然后我返回视图(模型)。
控件按照我的预期呈现。问题是,当我单击提交时,我的 Model.CostCenterId 为 0,而 Model.CostCenterList 为 NULL。我似乎无法从控件中取回所选值。
【问题讨论】:
标签: asp.net-mvc