Solved

Need help with MVC 4 Entity Framework

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

22 Experts available now in Live!

Get 1:1 Help Now