任何建议都很棒!
我一直为ClassID获取null值。当我添加断点时,我可以看到正确传递的值ClassID,但是当我的程序返回以获取我创建的列表时,ClassID在进入该特定断点后返回null。我相信我的问题在我的控制器中,但我似乎无法找到问题。
这是我的DB文件。第一个数据集从存储过程中检索我的ClassID,下面的列表返回特定ClassID中的学生
public DataSet GetClass(string ClassID)
{
ClassModel classes = new ClassModel();
SqlCommand com = new SqlCommand("sp_ABEAttendanceEnrollment", ABEAttendanceDB);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@ClassID", ClassID);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds1 = new DataSet();
da.Fill(ds1);
return ds1;
}
public IEnumerable<ClassModel> ClassModel
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["ABEAttendanceDB"].ConnectionString;
List<ClassModel> students = new List<ClassModel>();
using (SqlConnection con = new SqlConnection(connectionString))
{
ClassModel classes = new ClassModel();
SqlCommand cmd = new SqlCommand("sp_ABEAttendanceEnrollment", ABEAttendanceDB);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ClassID", classes.ClassID);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds1 = new DataSet();
da.Fill(ds1);
ABEAttendanceDB.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
ClassModel student = new ClassModel();
student.ClassID = rdr["ClassID"].ToString();
student.SID = rdr["SID"].ToString();
student.FullName = rdr["FullName"].ToString();
students.Add(student);
}
ABEAttendanceDB.Close();
return students;
}
}
}
这是我的控制器
public class ClassController : Controller
{
DAL.DB dblayer = new DAL.DB();
public ActionResult Class(ClassModel ClassModel)
{
DataSet ds1 = dblayer.GetClass(ClassModel.ClassID);
ViewBag.general = ds1.Tables;
ViewBag.Message = TempData["Message"];
DB classes = new DB();
List<ClassModel> students = dblayer.ClassModel.ToList();
return View();
}
}
答案 0 :(得分:0)
您的DAL代码有点奇怪。肯定GetClass
方法应该返回IEnumerable<ClassModel>
?否则,看起来你最终执行sp_ABEAttendanceEnrollment
两次没有真正的理由。至少可以说,将SQL查询放入getter是相当不寻常的。它还不清楚为什么您需要两份数据副本(一份作为DataTable
通过ViewBag.general
的集合,一份作为List<ClassModel>
通过students
变量
我不确定你的DAL课程上是否需要ClassModel
属性。只需让GetClass
方法直接将数据读入IEnumerable<Class>
,然后将其返回给控制器。然后,您可以将学生列表作为视图模型返回到控制器,如下所示:
DAL:
public List<ClassModel> GetClass(string ClassID)
{
List<ClassModel> students = new List<ClassModel>();
SqlCommand cmd = new SqlCommand("sp_ABEAttendanceEnrollment", ABEAttendanceDB);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ClassID", ClassID);
ABEAttendanceDB.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
ClassModel student = new ClassModel();
student.ClassID = rdr["ClassID"].ToString();
student.SID = rdr["SID"].ToString();
student.FullName = rdr["FullName"].ToString();
students.Add(student);
}
ABEAttendanceDB.Close();
return students;
}
控制器:
public class ClassController : Controller
{
DAL.DB dblayer = new DAL.DB();
public ActionResult Class(ClassModel ClassModel)
{
ViewBag.Message = TempData["Message"];
List<ClassModel> students = dblayer.getClass(ClassModel.ClassID);
return View(students);
}
}
视图将以:
开头@model List<ClassModel>