我有父视图和部分视图,但是当它尝试从父视图加载局部视图时会出现以下错误
传递到字典中的模型项的类型为'System.Data.Objects.DataClasses.EntityCollection`1 [RolMVC3.Models.OFFICE]',但此字典需要“RolMVC3.Models.OFFICE”类型的模型项
局部视图:
@model RolMVC3.Models.OFFICE
@Html.HiddenFor(model => model.IdOffice)
@Html.HiddenFor(model => model.IdSCampus)
<div class="editor-label">
@Html.LabelFor(model => model.AddressOffice)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AddressOffice)
@Html.ValidationMessageFor(model => model.AddressOffice)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PhoneOffice)
</div>
<div class="editor-field">
@Html.EditorFor(model => model..PhoneOffice)
@Html.ValidationMessageFor(model => model..PhoneOffice)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailOffice)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailOffice)
@Html.ValidationMessageFor(model => EmailOffice)
</div>
父视图:
@model RolMVC3.Models.CAMPUS_UNIVERSITY
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<h2> @ViewBag.University.Name - @ViewBag.Campus.NameCity </h2>
<fieldset>
<legend>MODIFY OFFICE</legend>
@Html.HiddenFor(model => model.IdUniversidty)
@Html.HiddenFor(model => model.IdCityCampus)
@Html.HiddenFor(model => model.IdCampus)
<div class="editor-label">
@Html.LabelFor(model => model.AddressCampus)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AddressCampus)
@Html.ValidationMessageFor(model => model.AddressCampus)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PhoneCampusSede)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PhoneCampus)
@Html.ValidationMessageFor(model => model.PhoneCampus)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EamailCampus)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EamailCampus)
@Html.ValidationMessageFor(model => model.EamailCampus)
</div>
<fieldset>
<legend>DATA</legend>
@Html.Partial("_Office", Model.OFFICE)
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
控制器:
public ActionResult Edit()
{
decimal id;
id = (decimal)Session["Offi"];
ViewBag.University = (from c in db.OFFICE
join s in db.CAMPUS_UNIVERSITY on c.IdCampus equals s.IdCampus
join u in db.UNIVERSIDTY on s.IdUniversity equals u.IdUniversity
where c.IdOffice == id
select u).Single();
ViewBag.Campus = (from c in db.OFFICE
join s in db.CAMPUS_UNIVERSITY on c.IdCampus equals s.IdCampus
join ci in db.CIUDAD on s.IdCaityCampus equals ci.IdCity
where c.IdOffice == id
select ci).Single();
OFFICE office = db.OFFICE.Single(c => c.IdOffice == id);
CAMPUS_UNIVERSITY campus_university = db.CAMPUS_UNIVERSITY.Single(s => s.IdSede == office.IdCampus);
return View(campus_university);
}
祝福
答案 0 :(得分:1)
您能展示RolMVC3.Models.OFFICE和CAMPUS_UNIVERSITY对象的样子吗?我猜你的CAMPUS_UNIVERSITY模型中有一些奇怪的东西。 .OFFICE字段是否紧密转换为OFFICE对象还是可枚举的?你可以通过在视图中传入Model.OFFICE [0]来快速测试它。
答案 1 :(得分:0)
看起来你传递的是一种不同于预期的类型。从您的角度来看,我可以看出该视图期待的是“OFFICE”类型的对象。您确定传递给Partial View调用的模式是否属于同一类型?或者你是否通过了一份办公室名单?
答案 2 :(得分:0)
你的控制器有代码
OFFICE office = db.OFFICE.Single(c => c.IdOffice == id);
CAMPUS_UNIVERSITY campus_university = db.CAMPUS_UNIVERSITY
.Single(s => s.IdSede == office.IdCampus);
但您的视图仅使用模型CAMPUS_UNIVERSITY
。我假设CAMPUS_UNIVERSITY.Office
属性是EntityCollection<OFFICE>
,它与视图对Office
的要求不匹配。
一种解决方案是显示所有办公室:
@foreach(var office in Model.OFFICE)
{
@Html.Partial("_Office", office)
}
或另一个是实际使用您在控制器中创建的Office
控制器(添加)
ViewBag.Office = db.OFFICE.Single(c => c.IdOffice == id);
查看(更改)
@Html.Partial("_Office", ViewBag.Office)