asp.net-mvc - ASP.NET MVC - ICollection<T> 映射到 Enum 的 EditorTemplate 问题

标签 asp.net-mvc asp.net-mvc-3 razor model-binding

我有一个 ASP.NET MVC 3 (Razor) 网站,以及一个名为 Review 的(简化)模型:

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)。

总结一下 - 单个评论许多建议,并且每个建议都映射到特定的枚举类型,我公开了钩子(Hook)属性以简化模型绑定(bind)/代码。

所以,进入 View :

@Html.EditorFor(model => model.Recommendations, "Recommendations")

非常简单。

现在,对于编辑器模板,我想为每个可能的 RecommendationType (枚举)显示一个复选框,如果模型有该推荐(例如在编辑 View 上),我会选中该框。

这是我所拥有的:

@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 。通常,这需要一个基于模型的表达式,但我不确定如何根据当前循环的枚举值绑定(bind)到钩子(Hook)属性。例如,它需要动态执行 @Html.CheckBoxFor(x => x.RecommendationOne) , @Html.CheckBoxFor(x => x.RecommendationTwo)

我当前的解决方案(有效),涉及手动构建 <input> (包括隐藏字段)。

但由于我刚刚掌握编辑器模板的窍门,希望有经验的人可以给我指出“强类型”的方向。

或者有更好的方法(HTML Helper)我可以做到这一点吗?

最佳答案

我将首先为该场景引入适当的 View 模型:

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; }
}

然后是 Controller :

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, 
                    IsChecked = true 
                },
            }
        };
        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");
    }
}

和相应的 View (~/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>

现在 View 代码已按其应有的方式进行了清理。没有 if、没有循环、没有 LINQ、没有反射,这是 Controller /映射器层的责任。因此,每次您发现自己在 View 中编写了一些高级 C# 逻辑时,我都会建议您重新考虑 View 模型并根据需要进行调整。这就是 View 模型的目的:尽可能接近 View 逻辑。

关于asp.net-mvc - ASP.NET MVC - ICollection<T> 映射到 Enum 的 EditorTemplate 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4652457/

相关文章:

c# - 不会调用 HttpPost

asp.net-mvc - asp-net core2新的web项目问题

asp.net - 在 MVC 3 中的 url 中传递多个参数

jquery - 使用AngularJS双击获取li的子div id

razor - MVC4 本地化。从 View 访问 resx

c# - 你如何在 Nhibernate 中进行版本控制?

javascript - 部分 View 无法正常工作

asp.net-mvc - IIS将MVC中的静态文件理解为动态内容

asp.net-mvc-3 - MVC编辑器问题对于命名模板

asp.net-mvc - 如何将 json 对象从 Javascript 传递到 asp.net mvc Controller