public class Review { public int ReviewId { get; set; } public bool RecommendationOne { // hook property - gets/set values in the ICollection } public bool RecommendationTwo { // etc } public ICollection<Recommendation> Recommendations { get; set; } }
建议如下:
public class Recommendation { public byte RecommendationTypeId }
我还有一个名为RecommendationType的枚举,用于将上述建议映射到. (基于RecommendationTypeId).
总而言之 – 单个Review有很多Recommendations,并且每个Recommendations都映射到特定的枚举类型,我公开了钩子属性以简化模型绑定/代码.
所以,在视图上:
@Html.EditorFor(model => model.Recommendations,"Recommendations")
很简单.
现在,对于编辑器模板,我想显示每个可能的RecommendationType(枚举)的复选框,如果模型有该推荐(例如编辑视图),我选中该复选框.
这就是我所拥有的:
@model IEnumerable<xxxx.DomainModel.Core.Posts.Recommendation> @using xxxx.DomainModel.Core.Posts; @{ Layout = null; } <table> @foreach (var rec in Enum.GetValues(typeof(RecommendationType)).Cast<RecommendationType>()) { <tr> <td> @* If review contains this recommendation,check the Box *@ @if (Model != null && Model.Any(x => x.RecommendationTypeId == (byte)rec)) { @* How do i create a (checked) checkBox here? *@ } else { @* How do i created a checkBox here? *@ } @rec.ToDescription() </td> </tr> } </table>
正如评论所示 – 我不知道如何使用@ Html.CheckBoxFor.通常,它采用基于模型的表达式,但我更确定如何基于当前循环的枚举值绑定到hook属性.例如,它需要动态地执行@ Html.CheckBoxFor(x => x.RecommendationOne),@ Html.CheckBoxFor(x => x.RecommendationTwo)等.
我现有的解决方案(有效)涉及手动构建< input>. (包括隐藏的字段).
但是,由于我只是掌握了编辑器模板,希望有经验的人可以指出我处于“强烈打字”的方向.
或者有更好的方式(HTML Helper)我能做到这一点吗?
解决方法
public enum RecommendationType { One,Two,Three } public class Reviewviewmodel { public IEnumerable<Recommendationviewmodel> Recommendations { get; set; } } public class Recommendationviewmodel { public RecommendationType RecommendationType { get; set; } public bool IsChecked { get; set; } }
然后是控制器:
public class HomeController : Controller { public ActionResult Index() { // TODO: query the repository to fetch your model // and use AutoMapper to map between it and the // corresponding view model so that you have a true/false // for each enum value var model = new Reviewviewmodel { Recommendations = new[] { new Recommendationviewmodel { RecommendationType = RecommendationType.One,IsChecked = false },new Recommendationviewmodel { RecommendationType = RecommendationType.Two,IsChecked = true },new Recommendationviewmodel { RecommendationType = RecommendationType.Three,} }; return View(model); } [HttpPost] public ActionResult Index(Reviewviewmodel model) { // Here you will get for each enum value the corresponding // checked value // TODO: Use AutoMapper to map back to your model and persist // using a repository return RedirectToAction("Success"); } }
和相应的视图(〜/ Views / Home / Index.cshtml):
@model YourAppName.Models.Reviewviewmodel @{ ViewBag.Title = "Index"; } @using (Html.BeginForm()) { @Html.EditorFor(model => model.Recommendations) <input type="submit" value="Go" /> }
最后是编辑模板(〜/ Views / Home / EditorTemplates / Recommendationviewmodel.cshtml)
@model YourAppName.Models.Recommendationviewmodel <div> @Html.HiddenFor(x => x.RecommendationType) @Model.RecommendationType @Html.CheckBoxFor(x => x.IsChecked) </div>
现在,视图代码将按原样清理.没有ifs,没有循环,没有LINQ,没有反射,这是控制器/映射器层的责任.因此,每当您发现自己在视图中编写一些高级C#逻辑时,我建议您重新考虑视图模型并根据需要进行调整.这就是视图模型的用途:尽可能接近视图逻辑.