Punya Munasinghe
Punya Munasinghe

Reputation: 285

How to solve type convertion error in ASP.NET C#?

Here is my Model

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

namespace TheFoody.Models
{
    public class ManageViewModel
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Email { get; set; }

        public string Phone { get; set; }

        public string Photo { get; set; }

        public string Address { get; set; }

        public string City { get; set; }

        public int PostCode { get; set; }

        public string District { get; set; }

        public string UserType { get; set; }

        public string Status { get; set; }
    }
}

Here is my Context

    namespace TheFoody.DataAccess
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class TheFoodyContext : DbContext
    {
        public TheFoodyContext()
            : base("name=TheFoodyContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Menu> Menus { get; set; }
        public virtual DbSet<User> Users { get; set; }
        public virtual DbSet<Category> Categories { get; set; }
        public virtual DbSet<Restaurant> Restaurants { get; set; }
        public virtual DbSet<Restaurant_Type> Restaurant_Type { get; set; }
    }
}

Here is My Controller

 using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TheFoody.DataAccess;
using TheFoody.Models;

namespace TheFoody.Controllers
{
    public class ManageController : Controller
    {
        public ActionResult Manage()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Manage(ManageViewModel manageviewmodel)
        {
            TheFoodyContext db = new TheFoodyContext();
            ManageViewModel user_to_update = db.Users.SingleOrDefault(s => s.email == manageviewmodel.Email);

            return View(manageviewmodel);
        }
    }
}

But In here when i'm trying to code this part "ManageViewModel user_to_update = db.Users.SingleOrDefault(s => s.email == manageviewmodel.Email);" It will give me highlighted error called "Cannot implicitly convert type 'TheFoody.DataAccess.User' to 'TheFoody.Models.ManageViewModel'"

I'm very new to this environment and i don't know why this error coming like these.

Upvotes: 2

Views: 86

Answers (2)

Draco
Draco

Reputation: 16364

That's because you cannot directly cast a type of User to ManageViewModel

You can fix the issue by assigning the result of the query to a type User and then iterate the properties of the User type and assign them to the relevant property of a ManageViewModel type.

Example:

User user_to_update = db.Users.SingleOrDefault(s => s.email == manageviewmodel.Email);

    if (user != null)
    {
        var manageViewModel = new ManageViewModel()

        manageViewModel.FirstName = user.FirstName;
        manageViewModel.LastName = user.LastName;
    }
    else {
     //Manage null exception here
    }

...And so on

Upvotes: 2

CodeCaster
CodeCaster

Reputation: 151588

db.Users is a DbSet<TheFoody.DataAccess.User>, and the SingleOrDefault() returns one such a user, or null when no user was found by that email address.

An instance of that class cannot be implicitly converted to the requested ManageViewModel, you need to map it:

DataAccess.User user = db.Users.SingleOrDefault(...);
if (user == null)
{
    // show error page telling that the POSTed email address is not known
}

ManageViewModel user_to_update = new ManageViewModel 
{
    FirstName = user.FirstName,
    LastName = user.LastName,
    // ...
}
return View(user_to_update);

You can use AutoMapper to do the mapping in a more convenient way.

Although, it seems like you want to update the database with the model sent through POST, then you need to change the logic: you need to assign the user's properties from the received manageviewmodel:

// read from database
DataAccess.User user = db.Users.SingleOrDefault(...);

if (user == null)
{
    // show error page telling that the POSTed email address is not known
}

// update from POST
user.FirstName = manageviewmodel.FirstName;
user.LastName = manageviewmodel.LastName;
// ...

// persist to database
db.SaveChanges();

Upvotes: 5

Related Questions