无法隐式转换类型'MusicStore.Models.student'到'MusicStore.Controllers.student [英] Cannot implicitly convert type 'MusicStore.Models.student' to 'MusicStore.Controllers.student
问题描述
示范code
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.ComponentModel.DataAnnotations;
使用System.Data.Entity的;命名空间MusicStore.Models
{
公共类学生
{
[键]
公众诠释StdID {搞定;组; }
公共字符串StdName {搞定;组; }
公共字符串StdGender {搞定;组; } } 公共类StudentDbContext:的DbContext
{
公共DbSet<学员GT;学生{搞定;组;} }
}
控制器
使用系统;
使用System.Collections.Generic;
使用System.Data这;
使用System.Data.Entity的;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用MusicStore.Models;命名空间MusicStore.Controllers
{
公共类学生:控制器
{
私人StudentDbContext DB =新StudentDbContext(); //
// GET:/学生/ 公众的ActionResult指数()
{
返回查看(db.students.ToList());
} //
// GET:/学生/详细资料/ 5 公众的ActionResult详细信息(INT ID = 0)
{
学生学生= db.students.Find(ID);
如果(学生== NULL)
{
返回HttpNotFound();
}
返回查看(学生);
} //
// GET:/学生/创建 公众的ActionResult的Create()
{
返回查看();
} //
// POST:/学生/创建 [HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult创建(学生学生)
{
如果(ModelState.IsValid)
{
db.students.Add(学生);
db.SaveChanges();
返回RedirectToAction(「指数」);
} 返回查看(学生);
} //
// GET:/学生/编辑/ 5 公众的ActionResult编辑(INT ID = 0)
{
学生学生= db.students.Find(ID);
如果(学生== NULL)
{
返回HttpNotFound();
}
返回查看(学生);
} //
// POST:/学生/编辑/ 5 [HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult编辑(学生学生)
{
如果(ModelState.IsValid)
{
db.Entry(学生).STATE = EntityState.Modified;
db.SaveChanges();
返回RedirectToAction(「指数」);
}
返回查看(学生);
} //
// GET:/学生/删除/ 5 公众的ActionResult删除(INT ID = 0)
{
学生学生= db.students.Find(ID);
如果(学生== NULL)
{
返回HttpNotFound();
}
返回查看(学生);
} //
// POST:/学生/删除/ 5 [HttpPost,ActionName(删除)]
[ValidateAntiForgeryToken]
公众的ActionResult DeleteConfirmed(INT ID)
{
学生学生= db.students.Find(ID);
db.students.Remove(学生);
db.SaveChanges();
返回RedirectToAction(「指数」);
} 保护覆盖无效的Dispose(BOOL处置)
{
db.Dispose();
base.Dispose(处置);
}
}
}
错误出现
错误3无法隐式转换类型'MusicStore.Models.student来
MusicStore.Controllers.studentC:\\用户\\ ABC \\文档\\ Visual Studio中
2013 \\项目\\ MusicStore \\ MusicStore \\ \\控制器29 student.cs 31 MusicStore
错误4的最佳重载的方法匹配
System.Data.Entity.DbSet.Add(MusicStore.Models.student)'
有一些无效参数C:\\用户\\ ABC \\文档\\ Visual Studio中
2013 \\项目\\ MusicStore \\ MusicStore \\ \\控制器54 student.cs 17 MusicStore
块引用>解决方案您有名称的冲突,作为你的控制器严重命名。里面你叫
学生
当你这样做的控制器:学生学生= db.students.Find(ID);
您是说这个变量是一个控制器,然后尝试把你的学生进去。
如果你给它的
控制器
后缀命名控制器的问题应该消失:公共类StudentController:控制器{...}
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); } } }
error appear
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 The 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
解决方案You have a clash of names as your controller is badly named. Inside your controller called
student
when you do this:student student = db.students.Find(id);
You are saying that the variable is a controller, then you try to put your student into it.
The problem should go away if you rename the controller by giving it the
Controller
suffix:public class StudentController : Controller { ... }
这篇关于无法隐式转换类型'MusicStore.Models.student'到'MusicStore.Controllers.student的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!