2
votes

I need pass two models in the Edit page (because I want to build a MVC 4 project with ViewModel), but when I try insert the view model in the edit page, I have the follow error:

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.People_E82A8FE6694DFF4D5ED1869045FE3E0A1855CC3CA65B873F5F1556DABC2DC9F4', but this dictionary requires a model item of type 'MVC_ViewModel.Models.ViewModel'.

Edit page (view):

@model MVC_ViewModel.Models.ViewModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>People</legend>

        @Html.HiddenFor(model => model.People.PeopleID)

        <div class="editor-label">
            @Html.LabelFor(model => model.People.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.People.Name)
            @Html.ValidationMessageFor(model => model.People.Name)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

Index page (view):

@model IEnumerable<MVC_ViewModel.People>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.PeopleID }) |
            @Html.ActionLink("Details", "Details", new { id=item.PeopleID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.PeopleID })
        </td>
    </tr>
}

</table>

ViewModel class (Model):

namespace MVC_ViewModel.Models
{
    public class ViewModel
    {
        public Car Car { get; set; }
        public List<Car> Cars { get; set; }
        public People People { get; set; }
        public List<People> Peoples { get; set; }
        public Detail Detail { get; set; }
        public List<Detail> Details { get; set; }
    }
}

CRUD (controller):

public ActionResult Index()
{
    return View(db.People.ToList());
}

...

public ActionResult Edit(int id = 0)
{
    People people = db.People.Find(id);
    if (people == null)
    {
        return HttpNotFound();
    }
    return View(people);
}

//
// POST: /CRUDViewModel/Edit/5

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(People people)
{
    if (ModelState.IsValid)
    {
        db.Entry(people).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(people);
}

I need understand the error, if anybody can help me. Thanks!

1

1 Answers

3
votes

You've specified in your ASP.NET Edit page that your model type is ViewModel:

@model MVC_ViewModel.Models.ViewModel

You then pass it a People object. Nowhere in the code you have provided do you use ViewModel.

If you were to change this to:

@model MVC_ViewModel.People

and change the references from model.People.* to model.*, then the error should go away.

However, I'd suggest you should be returning a PeopleViewModel and mapping various properties to this. It may be useful to go and read some blogs, tutorials and examples of how best to build this.