caitlinp
caitlinp

Reputation: 183

Cannot implicitly convert type MVC.Table1 to MVC.Models.prodInfo

I am working with ASP.Net MVC, just a beginner. Trying to get a good understanding of it. However I am getting this error:

Cannot implicitly convert type MVC.Table1 to MVC.Model.prodInfo

Here is my code:

Model

namespace MVC.Models
{
    public class Sample
    {
        Database_Entity db;

        public Sample()
        {
            db = new Database_Entity();
        }

        public List<prodInfo> getStuff()
        {

            var que = (from wr in db.Table1
                       join wrc in db.Table2 on wr.ID equals wrc.ID
                       join cd in db.Table3 on wrc.ID equals cd.ID
                       select new prodInfo
                       {
                           Short_ID = wr.ID,
                           Planned_Date = cd.Planned_Date.ToString(),
                           Actual_Date = cd.Actual_Date.ToString()
                       }).ToList();
            return que;
        }
    }

    public class prodInfo
    {
        public string Short_ID { get; set; }
        public string Planned_Date { get; set; }
        public string Actual_Date { get; set; }
    }
}

Controller

This is where the error occurs in the Edit(String ID) method...

namespace MVC.Controllers
{
    public class HelloController : Controller
    {
        Database_Entity db;
        // GET: Hello
        public ActionResult Index()
        {
            Sample sam = new Sample();
            return View(sam.getStuff());
        }

        public ActionResult Edit(string ID)
        {
            if (ID == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
            }
            prodInfo item = db.Table1.Find(ID);
            if (item == null)
            {
                return HttpNotFound();
            }
            return View(item);
        }

        [HttpPost]
        public ActionResult Edit(prodInfo model)
        {
            if (ModelState.IsValid)
            {
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                return RedirectToAction("Index");
            }

            return View(model);
        } 

    }

prodInfo item = db.Table1.Find(ID); This line is causing the error. I don't understand what is wrong, If anyone can help me out that would be great. If needed, I will add the View class in the edits.

Upvotes: 1

Views: 65

Answers (2)

AmirReza-Farahlagha
AmirReza-Farahlagha

Reputation: 1212

Well! When you want to get data from database using Entity-Framework, the return type of data is type of DatabaseModel.

For Fixed this you have to get data like var and map to your object. like:

namespace MVC.Controllers
{
    public class HelloController : Controller
    {
        Database_Entity db;
        // GET: Hello
        public ActionResult Index()
        {
            Sample sam = new Sample();
            return View(sam.getStuff());
        }

        public ActionResult Edit(string ID)
        {
            if (ID == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
            }
            var result = db.Table1.Find(ID);
            if (item == null)
            {
                return HttpNotFound();
            }
            return View(new prodInfo(){
                                /////
                                });
        }

        [HttpPost]
        public ActionResult Edit(prodInfo model)
        {
            if (ModelState.IsValid)
            {
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                return RedirectToAction("Index");
            }

            return View(model);
        } 

    }

Upvotes: 0

LazZiya
LazZiya

Reputation: 5729

Create a new method in your Sample class:

    public prodInfo GetProdInfo(string ID)
    {
        var pInfo = (from wr in db.Table1
                   join wrc in db.Table2 on wr.ID equals wrc.ID
                   join cd in db.Table3 on wrc.ID equals cd.ID
                   select new prodInfo
                   {
                       Short_ID = wr.ID,
                       Planned_Date = cd.Planned_Date.ToString(),
                       Actual_Date = cd.Actual_Date.ToString()
                   }).FirstOrDefault(x=>x.Short_ID == ID);
        return pInfo;
    }

and modify your Edit method in HomeController by calling the new method:

var result = sam.GetProdInfo(ID);

Upvotes: 1

Related Questions