Cannot implicitly convert type "MusicStore.Models.student" to "MusicStore.Controllers.student"

Model code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace MusicStore.Models
{
    public class student
    {
        [Key]
        public int StdID { get; set; }
        public string StdName { get; set; }
        public string StdGender { get; set; }

    }

    public class StudentDbContext: DbContext
    {
        public DbSet<student> students {get; set;}

    }
}

      

controller

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

namespace MusicStore.Controllers
{
    public class student : Controller
    {
        private StudentDbContext db = new StudentDbContext();

        //
        // GET: /student/

        public ActionResult Index()
        {
            return View(db.students.ToList());
        }

        //
        // GET: /student/Details/5

        public ActionResult Details(int id = 0)
        {
            student student = db.students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // GET: /student/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /student/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(student student)
        {
            if (ModelState.IsValid)
            {
                db.students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(student);
        }

        //
        // GET: /student/Edit/5

        public ActionResult Edit(int id = 0)
        {
            student student = db.students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /student/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

        //
        // GET: /student/Delete/5

        public ActionResult Delete(int id = 0)
        {
            student student = db.students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /student/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            student student = db.students.Find(id);
            db.students.Remove(student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

      

an error

Error 3 Cannot implicitly convert type "MusicStore.Models.student" to "MusicStore.Controllers.student" c: \ users \ abc \ documents \ visual studio 2013 \ Projects \ MusicStore \ MusicStore \ Controllers \ student.cs 29 31 MusicStore

Error 4 Best overloaded method match for 'System.Data.Entity.DbSet.Add (MusicStore.Models.student)' has some invalid arguments c: \ users \ abc \ documents \ visual studio 2013 \ Projects \ MusicStore \ MusicStore \ Controllers \ student.cs 54 17 MusicStore

0


source to share


1 answer


You have a naming clash as your controller is badly named. Inside your controller is called student

when you do this:

student student = db.students.Find(id);

      

You say a variable is a controller then you are trying to put your student in it.



The problem should go away if you rename the controller by giving it a suffix Controller

:

public class StudentController : Controller { ... }

      

+1


source







All Articles