加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET MVC – 用于ICollection的EditorTemplat

发布时间:2020-12-16 00:01:57 所属栏目:asp.Net 来源:网络整理
导读:我有一个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 { /
我有一个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).

总而言之 – 单个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#逻辑时,我建议您重新考虑视图模型并根据需要进行调整.这就是视图模型的用途:尽可能接近视图逻辑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读