问题 - C#MVC代码首先将空值插入表中?需要帮助! [英] Question - C# MVC Code First Inserting Null Values Into Tables? Need Assistance!

查看:50
本文介绍了问题 - C#MVC代码首先将空值插入表中?需要帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我最近开始尝试通过Patrick Washignton教程使用C#MVC进行编码。我正在尝试使用学生表,课程表和问题表生成数据库,但是,即使正在创建表,插入的值也为空,
学生名称甚至没有显示在views / cshtml中页面一旦我调试starwithoutdebugging。我怎样才能使这些值实际插入表中?  



以下是项目中每个类的代码。第一个是学生班,我想使用System制作表格


 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

namespace FirstAspProject.Models
{
public class Student
{
public int StudentId {get;组; }
公共字符串LastName {get;组; }
公共字符串FirstName {get;组; }
public DateTime EnrollmentDate {get;组; }
public ICollection< Enrollment> EnrollmentList {get;组; }
}
}

第二个是我想要制作表格的课程类

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

namespace FirstAspProject.Models
{
public class课程
{
public int CourseId {get;组; }
公共字符串CourseName {get;组; }
public int TotalCredits {get;组; }
public ICollection< Enrollment> EnrollmentList {get;组; }
}
}

第三个是注册类我是用

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

namespace FirstAspProject.Models
{
public class Enrollment
{
public int EnrollmentId {get; set;}
public int CourseId {get;组; }
public int StudentId {get;组; }
公共小数?等级{得到;组; }
公共学生{get;组; }
public课程{get;设置;}

}
}





我在WebConfig文件中初始化了数据库名称和.mdf,并从DBContext扩展了一个类

使用系统; 
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Conventions;
使用System.Linq;
使用System.Web;

名称空间FirstAspProject.Models
{
公共类SchoolContext:DbContext
{
public DbSet< Student> StudentList {get;组; }
public DbSet< Course> CourseList {get;组; }
public DbSet< Enrollment> EnrollmentList {get; set;}


}

然后我在global.asax文件中指定了初始化并创建了一个初始化器来将种子数据添加到DataBasa。以下是我将对象和属性分配给数据库表。


使用System; 
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;

名称空间FirstAspProject.Models
{
公共类SchoolInitializer:DropCreateDatabaseIfModelChanges< SchoolContext>
{
protected override void Seed(SchoolContext context)//在这里添加学生数据库或添加种子数据
{
var studentList = new List< Student>
{
new Student {FirstName =" James",LastName =" Dean"},
new Student {FirstName =" Lynda",LastName =" Thames"}

};
foreach(studentList中的var temp)
{
context.StudentList.Add(temp);
}
context.SaveChanges();

var courseList = new List< Course>
{
新课程{CourseName =" Java",TotalCredits = 4},
新课程{CourseName =" C#",TotalCredits = 4}
};
foreach(课程列表中的var temp)
{
context.CourseList.Add(temp);
}
context.SaveChanges();

var enrollmentList = new List< Enrollment>
{
新注册{StudentId = 1,CourseId = 1,年级= 3},
新注册{StudentId = 1,CourseId = 2,年级= 4}
};
foreach(entrymentList中的var temp)
{
context.EnrollmentList.Add(temp);
}
context.SaveChanges();
}

然后我使用FirstAspProject.Models在控制器中显示它

; 
使用FirstAspProject.ViewModels;
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;

命名空间FirstAspProject.Controllers
{
公共类XyzController:Controller
{
private SchoolContext db = new SchoolContext();
// GET:Xyz
public ActionResult Abc()
{
var studentList = db.StudentList.ToList(); //学生代表表基本上
返回View(studentList);
//课程数学=新课程();
//math.CourseName =" Math 101" ;;
//math.TotalCredits = 4;

//学生坎德拉里亚=新学生();
//candelaria.FirstName =" Christian" ;;
//candelaria.LastName =" Candelaria" ;;

//学生bryan =新学生();
//bryan.FirstName =" Bryan" ;;
//bryan.LastName =" Ortiz" ;;

//学生keila =新学生();
//keila.FirstName =" Keila" ;;
//keila.LastName =" Garcia" ;;

//列表<学生> studentList = new List< Student>();
//studentList.Add(candelaria);
//studentList.Add(bryan);
//studentList.Add(keila);

// Course_Students obj = new Course_Students();
//obj.course = math;
//obj.studentList = studentList;



}

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

并在视图


<下的Abc.chtml文件中调用数据库p>

 @ model IEnumerable< FirstAspProject.Models.Student> 
@ {
ViewBag.Title =" Abc" ;;
}

< h2> Abc.CSHTML视图< / h2>

@ *< h2>课程名称:@ Model.course.CourseName< / h2> * @

@foreach(模型中的var项目)
{
< h2>学生姓名:@ item.FirstName @ item.LastName< / h2>
}




正在创建数据库以及三个表,课程注册和学生,但这些表在所有字段上都有空值。我真的需要帮助才能继续本教程。有人可以帮忙吗?







解决方案

你好,


我最近开始尝试通过Patrick Washignton教程使用C#MVC进行编码。我正在尝试使用学生表,课程表和问题表生成数据库,但是,即使正在创建表,插入的值也为空,
学生名称甚至没有显示在views / cshtml中页面一旦我调试starwithoutdebugging。我怎样才能使这些值实际插入表中?  



以下是项目中每个类的代码。第一个是学生班,我想使用System制作表格

; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

namespace FirstAspProject.Models
{
public class Student
{
public int StudentId {get;组; }
公共字符串LastName {get;组; }
公共字符串FirstName {get;组; }
public DateTime EnrollmentDate {get;组; }
public ICollection< Enrollment> EnrollmentList {get;组; }
}
}

第二个是我想要制作表格的课程类

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

namespace FirstAspProject.Models
{
public class课程
{
public int CourseId {get;组; }
公共字符串CourseName {get;组; }
public int TotalCredits {get;组; }
public ICollection< Enrollment> EnrollmentList {get;组; }
}
}

第三个是注册类我是用

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

namespace FirstAspProject.Models
{
public class Enrollment
{
public int EnrollmentId {get; set;}
public int CourseId {get;组; }
public int StudentId {get;组; }
公共小数?等级{得到;组; }
公共学生{get;组; }
public课程{get;设置;}

}
}




我在WebConfig文件中初始化了DB名称和.mdf,并使用System从DBContext

创建了一个类; 
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Conventions;
使用System.Linq;
使用System.Web;

名称空间FirstAspProject.Models
{
公共类SchoolContext:DbContext
{
public DbSet< Student> StudentList {get;组; }
public DbSet< Course> CourseList {get;组; }
public DbSet< Enrollment> EnrollmentList {get; set;}


}

然后我在global.asax文件中指定了初始化并创建了一个初始化器将种子数据添加到DataBasa。以下是我将对象和属性分配给数据库表。

使用System; 
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;

名称空间FirstAspProject.Models
{
公共类SchoolInitializer:DropCreateDatabaseIfModelChanges< SchoolContext>
{
protected override void Seed(SchoolContext context)//在这里添加学生数据库或添加种子数据
{
var studentList = new List< Student>
{
new Student {FirstName =" James",LastName =" Dean"},
new Student {FirstName =" Lynda",LastName =" Thames"}

};
foreach(studentList中的var temp)
{
context.StudentList.Add(temp);
}
context.SaveChanges();

var courseList = new List< Course>
{
新课程{CourseName =" Java",TotalCredits = 4},
新课程{CourseName =" C#",TotalCredits = 4}
};
foreach(课程列表中的var temp)
{
context.CourseList.Add(temp);
}
context.SaveChanges();

var enrollmentList = new List< Enrollment>
{
新注册{StudentId = 1,CourseId = 1,年级= 3},
新注册{StudentId = 1,CourseId = 2,年级= 4}
};
foreach(entrymentList中的var temp)
{
context.EnrollmentList.Add(temp);
}
context.SaveChanges();
}

然后我使用FirstAspProject.Models在控制器中显示它

; 
使用FirstAspProject.ViewModels;
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;

命名空间FirstAspProject.Controllers
{
公共类XyzController:Controller
{
private SchoolContext db = new SchoolContext();
// GET:Xyz
public ActionResult Abc()
{
var studentList = db.StudentList.ToList(); //学生代表表基本上
返回View(studentList);
//课程数学=新课程();
//math.CourseName =" Math 101" ;;
//math.TotalCredits = 4;

//学生坎德拉里亚=新学生();
//candelaria.FirstName =" Christian" ;;
//candelaria.LastName =" Candelaria" ;;

//学生bryan =新学生();
//bryan.FirstName =" Bryan" ;;
//bryan.LastName =" Ortiz" ;;

//学生keila =新学生();
//keila.FirstName =" Keila" ;;
//keila.LastName =" Garcia" ;;

//列表<学生> studentList = new List< Student>();
//studentList.Add(candelaria);
//studentList.Add(bryan);
//studentList.Add(keila);

// Course_Students obj = new Course_Students();
//obj.course = math;
//obj.studentList = studentList;



}

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

并在视图


<下的Abc.chtml文件中调用数据库pre class ="prettyprint"> @ model IEnumerable< FirstAspProject.Models.Student>
@ {
ViewBag.Title =" Abc" ;;
}

< h2> Abc.CSHTML视图< / h2>

@ *< h2>课程名称:@ Model.course.CourseName< / h2> * @

@foreach(模型中的var项目)
{
< h2>学生姓名:@ item.FirstName @ item.LastName< / h2>
}


正在创建数据库和三个表,课程注册和学生,但表格在所有字段上都有空值。我真的需要帮助才能继续本教程。有人可以帮忙吗?






Hello,

I recently began trying to code with C# MVC via Patrick Washignton tutorials. I am trying to generate a Database with a Student Table, Course Table and Erollment Table, however, even though the tables are being created, the values inserted are null and the students names not even showing up in the views/cshtml page once I debug-starwithoutdebugging. How can I make it so that these values are actually inserted in the tables?  

Here is the code for each class in the Project. First one is for a Student class I want to make tables out of

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

namespace FirstAspProject.Models
{
    public class Student
    {
        public int StudentId { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public DateTime EnrollmentDate { get; set; }
        public ICollection<Enrollment>EnrollmentList { get; set; }
    }
}

Second is for a Course Class I want to make tables out of

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

namespace FirstAspProject.Models
{
    public class Course
    {
        public int CourseId { get; set; }
        public string CourseName { get; set; }
        public int TotalCredits { get; set; }
        public ICollection<Enrollment> EnrollmentList { get; set; }
     }
}

Third is for an Enrollment Class I was to make a table out of

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

namespace FirstAspProject.Models
{
    public class Enrollment
    {
        public int EnrollmentId{get; set;}
        public int CourseId { get; set; } 
        public int StudentId { get; set; } 
        public decimal? Grade { get; set; } 
        public Student student { get; set; } 
        public Course course { get;  set;} 
        
    }
}


I initialized the DB name and .mdf in the WebConfig file and made a class extending from DBContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace FirstAspProject.Models
{
    public class SchoolContext : DbContext
    {
        public DbSet<Student>StudentList { get; set; }
        public DbSet<Course> CourseList { get; set; }
        public DbSet<Enrollment> EnrollmentList { get; set;}

        
    }

Then I specified the initialization in the global.asax file and created an Initializer to add Seed Data to the DataBasa. Here is were I would assigned the objects and properties to the Database tables.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace FirstAspProject.Models
{
    public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
    {
        protected override void Seed(SchoolContext context) //adding student database here or adding seed data 
        {
            var studentList = new List<Student>
            {
                new Student { FirstName = "James", LastName = "Dean"},
                new Student { FirstName = "Lynda", LastName = "Thames"}

            };
            foreach (var temp in studentList)
            {
                context.StudentList.Add(temp); 
            }
            context.SaveChanges();
            
            var courseList = new List<Course>
            {
                new Course { CourseName = "Java", TotalCredits = 4},
                new Course { CourseName = "C#", TotalCredits = 4 }
            };
            foreach (var temp in courseList)
            {
                context.CourseList.Add(temp);
            }
            context.SaveChanges();

            var enrollmentList = new List<Enrollment>
            {
                new Enrollment {StudentId = 1, CourseId = 1, Grade = 3 },
                new Enrollment {StudentId = 1, CourseId = 2, Grade = 4 }
            };
            foreach (var temp in enrollmentList)
            {
                context.EnrollmentList.Add(temp);
            }
            context.SaveChanges();
        }

Then I displayed it in the controller

using FirstAspProject.Models;
using FirstAspProject.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FirstAspProject.Controllers
{
    public class XyzController : Controller
    {
        private SchoolContext db = new SchoolContext();
        // GET: Xyz
        public ActionResult Abc()
        {
            var studentList = db.StudentList.ToList(); //students represents table basically 
            return View(studentList);
            //Course math = new Course();
            //math.CourseName = "Math 101";
            //math.TotalCredits = 4;

            //Student candelaria = new Student();
            //candelaria.FirstName = "Christian";
            //candelaria.LastName = "Candelaria";

            //Student bryan = new Student();
            //bryan.FirstName = "Bryan";
            //bryan.LastName = "Ortiz";

            //Student keila = new Student();
            //keila.FirstName = "Keila";
            //keila.LastName = "Garcia";

            //List<Student> studentList = new List<Student>();
            //studentList.Add(candelaria);
            //studentList.Add(bryan);
            //studentList.Add(keila);

            //Course_Students obj = new Course_Students();
            //obj.course = math;
            //obj.studentList = studentList; 

    
            
        }

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

And called the Database in the Abc.chtml file under Views

@model IEnumerable<FirstAspProject.Models.Student>
@{
    ViewBag.Title = "Abc";
}

<h2>Abc.CSHTML UNDER VIEWS</h2>

@*<h2>Course Title : @Model.course.CourseName</h2>*@

@foreach (var item in Model)
{
    <h2> Student Name : @item.FirstName @item.LastName </h2>
}


The Database is being created along with three tables, Courses Enrollments and Students but the tables have null values on all fields. I really need help with this in order to continue the tutorial. Can anyone please assist?

解决方案

Hello,

I recently began trying to code with C# MVC via Patrick Washignton tutorials. I am trying to generate a Database with a Student Table, Course Table and Erollment Table, however, even though the tables are being created, the values inserted are null and the students names not even showing up in the views/cshtml page once I debug-starwithoutdebugging. How can I make it so that these values are actually inserted in the tables?  

Here is the code for each class in the Project. First one is for a Student class I want to make tables out of

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

namespace FirstAspProject.Models
{
    public class Student
    {
        public int StudentId { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public DateTime EnrollmentDate { get; set; }
        public ICollection<Enrollment>EnrollmentList { get; set; }
    }
}

Second is for a Course Class I want to make tables out of

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

namespace FirstAspProject.Models
{
    public class Course
    {
        public int CourseId { get; set; }
        public string CourseName { get; set; }
        public int TotalCredits { get; set; }
        public ICollection<Enrollment> EnrollmentList { get; set; }
     }
}

Third is for an Enrollment Class I was to make a table out of

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

namespace FirstAspProject.Models
{
    public class Enrollment
    {
        public int EnrollmentId{get; set;}
        public int CourseId { get; set; } 
        public int StudentId { get; set; } 
        public decimal? Grade { get; set; } 
        public Student student { get; set; } 
        public Course course { get;  set;} 
        
    }
}


I initialized the DB name and .mdf in the WebConfig file and made a class extending from DBContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace FirstAspProject.Models
{
    public class SchoolContext : DbContext
    {
        public DbSet<Student>StudentList { get; set; }
        public DbSet<Course> CourseList { get; set; }
        public DbSet<Enrollment> EnrollmentList { get; set;}

        
    }

Then I specified the initialization in the global.asax file and created an Initializer to add Seed Data to the DataBasa. Here is were I would assigned the objects and properties to the Database tables.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace FirstAspProject.Models
{
    public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
    {
        protected override void Seed(SchoolContext context) //adding student database here or adding seed data 
        {
            var studentList = new List<Student>
            {
                new Student { FirstName = "James", LastName = "Dean"},
                new Student { FirstName = "Lynda", LastName = "Thames"}

            };
            foreach (var temp in studentList)
            {
                context.StudentList.Add(temp); 
            }
            context.SaveChanges();
            
            var courseList = new List<Course>
            {
                new Course { CourseName = "Java", TotalCredits = 4},
                new Course { CourseName = "C#", TotalCredits = 4 }
            };
            foreach (var temp in courseList)
            {
                context.CourseList.Add(temp);
            }
            context.SaveChanges();

            var enrollmentList = new List<Enrollment>
            {
                new Enrollment {StudentId = 1, CourseId = 1, Grade = 3 },
                new Enrollment {StudentId = 1, CourseId = 2, Grade = 4 }
            };
            foreach (var temp in enrollmentList)
            {
                context.EnrollmentList.Add(temp);
            }
            context.SaveChanges();
        }

Then I displayed it in the controller

using FirstAspProject.Models;
using FirstAspProject.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FirstAspProject.Controllers
{
    public class XyzController : Controller
    {
        private SchoolContext db = new SchoolContext();
        // GET: Xyz
        public ActionResult Abc()
        {
            var studentList = db.StudentList.ToList(); //students represents table basically 
            return View(studentList);
            //Course math = new Course();
            //math.CourseName = "Math 101";
            //math.TotalCredits = 4;

            //Student candelaria = new Student();
            //candelaria.FirstName = "Christian";
            //candelaria.LastName = "Candelaria";

            //Student bryan = new Student();
            //bryan.FirstName = "Bryan";
            //bryan.LastName = "Ortiz";

            //Student keila = new Student();
            //keila.FirstName = "Keila";
            //keila.LastName = "Garcia";

            //List<Student> studentList = new List<Student>();
            //studentList.Add(candelaria);
            //studentList.Add(bryan);
            //studentList.Add(keila);

            //Course_Students obj = new Course_Students();
            //obj.course = math;
            //obj.studentList = studentList; 

    
            
        }

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

And called the Database in the Abc.chtml file under Views

@model IEnumerable<FirstAspProject.Models.Student>
@{
    ViewBag.Title = "Abc";
}

<h2>Abc.CSHTML UNDER VIEWS</h2>

@*<h2>Course Title : @Model.course.CourseName</h2>*@

@foreach (var item in Model)
{
    <h2> Student Name : @item.FirstName @item.LastName </h2>
}

The Database is being created along with three tables, Courses Enrollments and Students but the tables have null values on all fields. I really need help with this in order to continue the tutorial. Can anyone please assist?


这篇关于问题 - C#MVC代码首先将空值插入表中?需要帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆