使用Entity Framework代码优先方法阻止视图(MVC)

时间:2016-04-27 06:06:22

标签: entity-framework asp.net-mvc-4

我在View

上收到错误消息
  

传递到字典中的模型项的类型为' System.Collections.Generic.List' 1 [oneToOneRelationship.Student]',但此字典需要类型为&#39的模型项; System.Collections.Generic.IEnumerable' 1 [oneToOneRelationship.StudentDBContext]'

在运行应用程序时,数据库正在更新。

namespace oneToOneRelationship.Controllers    
{   
    public class StudentController : Controller    
    {    
        StudentDBContext objContext;     
        public StudentController()      
        {     
            objContext = new StudentDBContext();    
        }

        public ActionResult Index()
        {
            Student s = new Student
            {
                StudentId = 1,
                StudentName = "Hitesh",
                StudentAge = 24
            };
            StudentAccount sa = new StudentAccount { 
                             StudentName = "Sports account",
                             StudentAmount = 300,
                             student = s
            };

            objContext.Students.Add(s);
            objContext.StudentAccounts.Add(sa);
            objContext.SaveChanges();

            var result = from r in objContext.Students select r;
            var data = objContext.Students.ToList();
            return View(data);
        }
    }
}

0 个答案:

没有答案