Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with MVC 4 Entity Framework

Posted on 2013-06-14
4
Medium Priority
?
526 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 2000 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

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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