Solved

Need help with MVC 4 Entity Framework

Posted on 2013-06-14
4
508 Views
Last Modified: 2013-06-15
I am using a Model Wrapper to include three models into one view as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RescueFormsAdministration.Models
{
    public class CreateAccountViewModel
    {
        public Account AccountModel { get; set; }
        public Data DataModel { get; set; }
        public Phone PhoneModel { get; set; }
    }
}

I modified the Create ActionResult in the AccountsController to use the CreateAccountViewModel instead of Account. This works fine after I also modified the -CreateOrEdit.cshtml partial class to use the CreateAccountViewModel as well
        [HttpPost]
        public ActionResult Create(CreateAccountViewModel accountViewModel)
        {
            if (ModelState.IsValid)
            {
                context.Accounts.Add(accountViewModel.AccountModel);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(accountViewModel);
        }


 I am having trouble modifying the non-HttpPost Edit Edit ActionResult

        public ActionResult Edit(int id)
        {
            Account account = context.Accounts.Single(x => x.AccountsID == id);
            return View(account);
        }

The problem is when I change Account account to CreateAccountViewModel accountViewModel  I get a problem with this line:

CreateAccountViewModel accountViewModel = context.Accounts.Single(x => x.AccountsID == id);

I get the error:
“Cannot implicitly convert type 'RescueFormsAdministration.Models.Account' to 'RescueFormsAdministration.Models.CreateAccountViewModel'       

'RescueFormsAdministration.Models.Account' is one of the three Models that I have placed into the CreateAccountViewModel wrapper and 'RescueFormsAdministration.Models.CreateAccountViewModel' is of course the name of the Model Wrapper itself.

Can anyone please help me?
0
Comment
Question by:AmbassadorSpock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39250029
context.Accounts.Single return an Account. You're trying to assign the Account to a CreateAccountViewModel. They aren't the same thing. That's exactly what the error message is telling you. You need to assign the results of context.Accounts.Single to the AccountModel inside CreateAccountViewModel.

CreateAccountViewModel accountViewModel = new CreateAccountViewModel();
accountViewModel.AccountModel = context.Accounts.Single (...)
0
 

Author Comment

by:AmbassadorSpock
ID: 39250570
CraigWagner

OK, I am slowly getting there, this fixed my problem with...

public ActionResult Edit(int id)

now I have a prolem after the Edit with that I belive is related since I get the same error message

        [HttpPost]
        public ActionResult Edit(CreateAccountViewModel accountViewModel)

How can I award you the 500 points and create another question but get it to your attention since you were able to fix the first problem?
0
 

Author Comment

by:AmbassadorSpock
ID: 39250617
I figured out the problem, your solution helped quite a bit
0
 

Author Closing Comment

by:AmbassadorSpock
ID: 39250619
Great...I figured out the rest of my problems with your solution. Thank You
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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