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
Your controller has the code
But your View is ONLY using the model
CAMPUS_UNIVERSITY. I would assume that theCAMPUS_UNIVERSITY.Officeproperty is aEntityCollection<OFFICE>which does not match the view’s requirement ofOffice.One solution is to display all the offices:
or the other is to actually use the
Officeyou created in the controllerController (add)
View (change)