简体   繁体   English

传递一个空值

[英]Passing a null value

Any suggestions would be awesome! 任何建议都很棒!

I keep getting a null value for my ClassID. 我一直为我的ClassID获取空值。 When I add breakpoints, I can see the value ClassID being passed correctly, but when my program goes back to get a list I created, the ClassID returns null after stepping into that particular breakpoint. 添加断点时,可以看到正确传递了ClassID值,但是当我的程序返回以获取我创建的列表时,ClassID进入该特定断点后将返回null。 I believe my issue is in my controller, but I cant seem to find the issue. 我相信我的问题出在我的控制器上,但我似乎找不到该问题。

Here is my DB file. 这是我的数据库文件。 The first dataset retrieves my ClassID from a stored procedure and my list below that returns the students in a particular ClassID 第一个数据集从存储过程中检索我的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;
            }
        }

    }

Here is my controller 这是我的控制器

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();
    }


}   

Your DAL code is bit strange. 您的DAL代码有点奇怪。 Surely the GetClass method should return the IEnumerable<ClassModel> ? 当然, GetClass方法应该返回IEnumerable<ClassModel>吗? Otherwise it just looks like you end up executing sp_ABEAttendanceEnrollment twice for no real reason. 否则,看起来您最终sp_ABEAttendanceEnrollment执行sp_ABEAttendanceEnrollment两次sp_ABEAttendanceEnrollment Putting a SQL query in a getter is rather unusual, to say the least. 至少可以说,将SQL查询放入吸气剂是很不寻常的。 It's also not clear why you would need two copies of your data (one as a collection of DataTable via ViewBag.general and one as a List<ClassModel> via the students variable. 还不清楚为什么需要数据的两个副本(一个通过ViewBag.general作为DataTable的集合,另一个通过students变量作为List<ClassModel>

I'm not sure you need the ClassModel property on your DAL class at all. 我不确定您是否完全需要DAL类上的ClassModel属性。 Just make the GetClass method read the data into an IEnumerable<Class> directly, and return it to the controller. 只需使GetClass方法将数据直接读取到IEnumerable<Class> ,然后将其返回给控制器即可。 Then you can return the list of students to the controller as the model for your view, something like this: 然后,您可以将学生列表作为视图的模型返回给控制器,如下所示:

DAL: 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;
}

Controller: 控制器:

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);
    }
}

The view would then start with: 然后,该视图将从以下位置开始:

@model List<ClassModel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM