Solved

Need help with MVC 4 Entity Framework

Posted on 2013-06-14
4
490 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

19 Experts available now in Live!

Get 1:1 Help Now