構建查看模型來描述您的User
和Feature
對象。然後,您可以使用編輯器模板來顯示覆選框。
public class UserFeatureViewModel
{
public string Name { get; set; }
public IEnumerable<FeatureViewModel> Features { get; set; }
}
// and the FeatureViewModel
public class FeatureViewModel
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
我說構建,這樣做,在你的控制器,視圖模型工廠或維修。該Feature
實例這裏硬編碼的,但你想從數據庫檢索它們:
public class UserController : Controller
{
public ActionResult GetUser(string username)
{
var model = new UserFeatureViewModel
{
Name = username,
Features = new[]
{
new FeatureViewModel { Name = "Feature abc", IsSelected = true },
new FeatureViewModel { Name = "Feature def", IsSelected = false },
new FeatureViewModel { Name = "Feature xyz", IsSelected = true }
}
};
return View(model);
}
}
你可以用以後的行動與UserFeatureViewModel
參數更新等等
最後,編輯模板在~/Views/User/EditorTemplates/FeatureViewModel.cshtml
將繼承一個FeatureViewModel
,並使用Html.CheckBoxFor
幫手:
@model FeatureViewModel
<span>@Model.Name</span>
<span>@Html.CheckBoxFor(model => model.IsSelected)</span>
在您的形式(「父」視圖),使用EditorFor
助手來解析部分ial
@Html.EditorFor(model => model.Features)
Dude!太多的問題和想法。你的問題是什麼? – gdoron 2011-12-19 18:41:40