Reputation: 93
I have a parent view and a partial view, but when it try to load the partial view from the parent view get the following error
The model item passed into the dictionary is of type 'System.Data.Objects.DataClasses.EntityCollection`1[RolMVC3.Models.OFFICE]',but this dictionary requires a model item of type 'RolMVC3.Models.OFFICE'.
partial view:
@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>
parent view:
@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>
}
controller:
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);
}
blessings
Upvotes: 0
Views: 297
Reputation: 54618
Your controller has the code
OFFICE office = db.OFFICE.Single(c => c.IdOffice == id);
CAMPUS_UNIVERSITY campus_university = db.CAMPUS_UNIVERSITY
.Single(s => s.IdSede == office.IdCampus);
But your View is ONLY using the model CAMPUS_UNIVERSITY
. I would assume that the CAMPUS_UNIVERSITY.Office
property is a EntityCollection<OFFICE>
which does not match the view's requirement of Office
.
One solution is to display all the offices:
@foreach(var office in Model.OFFICE)
{
@Html.Partial("_Office", office)
}
or the other is to actually use the Office
you created in the controller
Controller (add)
ViewBag.Office = db.OFFICE.Single(c => c.IdOffice == id);
View (change)
@Html.Partial("_Office", ViewBag.Office)
Upvotes: 0
Reputation: 218722
It looks like you are passing a differnt type than what is expecting. From your view, i could undestand that the view is expecting an object of type "OFFICE". Are you sure the modal you are passing to the Partial View call is of same type ? Or are you passing a List of Offices ?
Upvotes: 0
Reputation: 590
Can you show what RolMVC3.Models.OFFICE and CAMPUS_UNIVERSITY objects look like? I'm guessing your CAMPUS_UNIVERSITY model has something weird going on in it. Is the .OFFICE field tightly cast as an OFFICE object or is it an enumerable? You could probably test this out quickly by passing in Model.OFFICE[0] in the view.
Upvotes: 1