Solved

MVC 4: Several Models in one Model causing an error

Posted on 2013-06-12
1
424 Views
Last Modified: 2013-06-12
I have several Models defined such as RescueFormsAdministration.Models.Account that I have given the name AccountModel and a wrapper Model RescueFormsAdministration.Models.CreateAccountViewModel that contains the Account and other Models in order to pass it to one View. This works until I run it and then I get the error below:
 
"The model item passed into the dictionary is of type 'RescueFormsAdministration.Models.Account', but this dictionary requires a model item of type 'RescueFormsAdministration.Models.CreateAccountViewModel'. "

Does anyone know how to fix the problem?
0
Comment
Question by:AmbassadorSpock
1 Comment
 

Accepted Solution

by:
AmbassadorSpock earned 0 total points
ID: 39242131
I found my own answer:

I made to modify the AccountController, it was:

        [HttpPost]
        public ActionResult Create(Account account)
        {
            if (ModelState.IsValid)
            {
                context.Accounts.Add(account);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(account);
        }

After I modified it to the below, it worked.

        [HttpPost]
        public ActionResult Create(CreateAccountViewModel CreateAccount)
        {
            if (ModelState.IsValid)
            {
                context.Accounts.Add(CreateAccount.AccountModel);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(CreateAccount);
        }
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now