【发布时间】:2015-09-19 21:36:35
【问题描述】:
我有两个视图,一个是会员类型列表,另一个是为会员类型创建。
两者都可以作为单独的视图正常工作,但我想将它们组合起来,以便有一个成员资格类型列表,然后是一个表单以在同一视图中创建一个。
我已将列表视图转换为部分视图以在创建视图中引用,但出现以下错误:
附加信息:传入字典的模型项是 '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);
}
列表视图模型是:
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")
}
【问题讨论】:
-
partialView 在
ListClubMembershipType传输什么类型? -
创建一个包含 2 个属性的第三个视图模型 - (比如)IEnumerable
MemershipList` 和 NewMembershipTypeViewModel NewMember并将其传递给视图。或者在主视图中,使用@Html.RenderAction()来生成partials
标签: c# asp.net-mvc