Solved

Need help with MVC 4 Entity Framework

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net VB.net Errors when sending data to SQL 5 20
IIS redirect 1 68
Regex validation 2 24
VB.net Move a class from Solution Items to a project 2 22
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…
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 …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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