我有两个视图,一个是成员类型列表,另一个是创建成员类型。
两者都可以作为单独的视图工作,但我希望将它们组合起来,以便有一个成员类型列表,后面跟一个表单在同一个视图中创建一个。
我已将列表视图转换为部分视图以在创建视图中引用,但我收到以下错误:
附加信息:传递到字典中的模型项是 类型'GRCWebApp.ViewModels.ListMembershipTypeViewModel',但是这个 字典需要类型的模型项 'System.Collections.Generic.IEnumerable`1 [GRCWebApp.ViewModels.ListMembershipTypeViewModel]'。
列表控制器是:
public ActionResult ListClubMembershipType(int clubId)
{
var types = from s in db.MembershipTypes
where (s.ClubId == clubId)
orderby s.Type
select s;
var model = types.Select(t => new ListMembershipTypeViewModel
{
Type = t.Type,
ClubId = clubId
});
return PartialView("_ListClubMembershipType", model);
}
创建控制器是:
public ActionResult AddClubMembershipType(NewMembershipTypeViewModel model, int clubId)
{
model.ClubId = clubId;
return View(model);
}
List Viewmodel是:
public class ListMembershipTypeViewModel
{
[Display(Name = "Type")]
public String Type { get; set; }
public int ClubId { get; set; }
}
创建Viewmodel是:
public class NewMembershipTypeViewModel
{
[Required]
[Display(Name = "Type")]
[StringLength(350, ErrorMessage = "Membership Type Name cannot be longer than 350 characters.")]
public String Type { get; set; }
[Display(Name = "Description")]
[StringLength(350, ErrorMessage = "Interest Name cannot be longer than 350 characters.")]
public String Description { get; set; }
public int ClubId { get; set; }
}
列表视图:
@model IEnumerable<GRCWebApp.ViewModels.ListMembershipTypeViewModel>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Type)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
</tr>
}
</table>
带部分参考的创建视图是:
@model GRCWebApp.ViewModels.NewMembershipTypeViewModel
@Html.Partial("_ListClubMembershipType", new GRCWebApp.ViewModels.ListMembershipTypeViewModel())
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
@Html.HiddenFor(model => model.ClubId)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:2)
有很多方法可以做到这一点,但为了获得最大的灵活性(允许你只渲染表单,或只是渲染集合以包含两者),那么你可以使用@Html.Action()
创建一个新的控制器方法
public ActionResult Combined(int clubId)
{
ViewBag.ClubID = clubId;
return View();
}
并在视图中
@Html.Action("ListClubMembershipType", new { clubId = ViewBag.ClubID })
@Html.Action("AddClubMembershipType", new { clubId = ViewBag.ClubID })
并从现有的“创建”视图中删除@Html.Partial()
注意:您的AddClubMembershipType()
不应包含NewMembershipTypeViewModel model
或者根据您现有的“创建”视图,将@Html.Partial()
替换为
@Html.Action("ListClubMembershipType", new { clubId = Model.ClubID })
答案 1 :(得分:1)
您的列表部分视图需要IEnumerable
ListMembershipTypeViewModel
:
@model IEnumerable<GRCWebApp.ViewModels.ListMembershipTypeViewModel>
但是你传递了一个ListMembershipTypeViewModel
:
@Html.Partial("_ListClubMembershipType", new GRCWebApp.ViewModels.ListMembershipTypeViewModel())
您需要将NewMembershipTypeViewModel
更改为以下内容:
public class NewMembershipTypeViewModel
{
[Required]
[Display(Name = "Type")]
[StringLength(350, ErrorMessage = "Membership Type Name cannot be longer than 350 characters.")]
public String Type { get; set; }
[Display(Name = "Description")]
[StringLength(350, ErrorMessage = "Interest Name cannot be longer than 350 characters.")]
public String Description { get; set; }
public int ClubId { get; set; }
public List<ListMembershipTypeViewModel> TypeList { get; set; }
}
使用您要显示的项目在控制器中填充该对象,然后将视图中的行更改为:
@Html.Partial("_ListClubMembershipType", model.TypeList)
答案 2 :(得分:0)
因为您视图中的模型不是IEnumerable。在另一个视图中,你得到了正确的答案。 在
的部分视图中更新您的代码zindex
到
@model GRCWebApp.ViewModels.NewMembershipTypeViewModel
或者您必须更改ListClubMembershipType的控制器内容。