RPM*_*984 5 asp.net-mvc model-binding razor asp.net-mvc-3
我有一个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; }
}
Run Code Online (Sandbox Code Playgroud)
建议 如下:
public class Recommendation
{
public byte RecommendationTypeId
}
Run Code Online (Sandbox Code Playgroud)
我还有一个名为RecommendationType的枚举,用于将上述建议映射到.(基于RecommendationTypeId).
总而言之 - 单个Review有许多Recommendations,并且每个Recommendations都映射到特定的枚举类型,我公开了钩子属性以简化模型绑定/代码.
所以,在视图上:
@Html.EditorFor(model => model.Recommendations, "Recommendations")
Run Code Online (Sandbox Code Playgroud)
很简单.
现在,对于编辑器模板,我想显示每个可能的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>
Run Code Online (Sandbox Code Playgroud)
正如评论所示 - 我不知道如何使用@Html.CheckBoxFor.通常,它采用基于模型的表达式,但我更确定如何基于当前循环的枚举值绑定到hook属性.例如,它需要做动态@Html.CheckBoxFor(x => x.RecommendationOne),@Html.CheckBoxFor(x => x.RecommendationTwo)等等.
我现有的解决方案(有效)涉及手动构建<input>(包括隐藏字段).
但是,由于我只是掌握了编辑器模板,希望有经验的人可以指出我处于"强烈打字"的方向.
或者有更好的方式(HTML Helper)我能做到这一点吗?
Dar*_*rov 10
我首先介绍一个适用于该场景的视图模型:
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; }
}
Run Code Online (Sandbox Code Playgroud)
然后控制器:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
和相应的视图(~/Views/Home/Index.cshtml):
@model YourAppName.Models.ReviewViewModel
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
@Html.EditorFor(model => model.Recommendations)
<input type="submit" value="Go" />
}
Run Code Online (Sandbox Code Playgroud)
最后是编辑模板(~/Views/Home/EditorTemplates/RecommendationViewModel.cshtml)
@model YourAppName.Models.RecommendationViewModel
<div>
@Html.HiddenFor(x => x.RecommendationType)
@Model.RecommendationType
@Html.CheckBoxFor(x => x.IsChecked)
</div>
Run Code Online (Sandbox Code Playgroud)
现在,视图代码将按原样清理.没有ifs,没有循环,没有LINQ,没有反射,这是控制器/映射器层的责任.因此,每当您发现自己在视图中编写一些高级C#逻辑时,我建议您重新考虑视图模型并根据需要进行调整.这就是视图模型的用途:尽可能接近视图逻辑.
| 归档时间: |
|
| 查看次数: |
2245 次 |
| 最近记录: |