Dropdownlistfor对象引用未设置为ModelState.IsValid之后的对象实例

时间:2015-11-29 12:41:05

标签: asp.net asp.net-mvc razor

DropdownListFor通过(ModelState.IsValid)时遇到问题。当它通过DropdownList时,会发生错误(Object reference not set to an instance of an object.

查看

视图的这一行出错

 @Html.DropDownListFor(model => model.si_g_code, new= SelectList(Model.Guardian,"g_ref_code","fullname"), "Select Guardian")
 @Html.ValidationMessageFor(model => model.si_g_code)

 @Html.DropDownListFor(model => model.si_gl_id, new SelectList(Model.GradeLevel,"gl_id","gl_name"), "Select Grade Level", new { id = "ddlGrade" })
 @Html.ValidationMessageFor(model => model.si_gl_id)

控制器

    [HttpGet]
    public ActionResult RegisterStudent()
    {
        Models.ModelActions action = new Models.ModelActions();
        var model = new acgs_qm.Models.CreateStudent
        {
            GradeLevel = action.getGrade(),
            Guardian = action.getGuardian(),
            si_id = action.getStudentNum()
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult RegisterStudent(CreateStudent Create)
    {
        acgs_qm.Models.ModelActions Ma = new acgs_qm.Models.ModelActions();
        if (ModelState.IsValid)
        {
            Ma.insertStudent(Create);
        }
        return View();
    }

模型

public class CreateStudent
{
    [DisplayAttribute(Name = "Student ID")]
    public string si_id { get; set; }
    [Required]
    [DisplayAttribute(Name = "First Name")]
    public string si_fname { get; set; }
    [Required]
    [DisplayAttribute(Name = "Middle Name")]
    public string si_mname { get; set; }
    [Required]
    [DisplayAttribute(Name = "Last Name")]
    public string si_lname { get; set; }
    [DataType(DataType.Text)]
    [Required]
    [DisplayAttribute(Name = "Contact Number")]
    public string si_contact_no { get; set; }
    [Required]
    [DisplayAttribute(Name = "Gender")]
    public string si_gender { get; set; }
    [Required]
    [DisplayAttribute(Name = "Civil Status")]
    public string si_civil_status { get; set; }
    [Required]
    [DisplayAttribute(Name = "Birthdate")]
    public string si_birthdate { get; set; }
    [Required]
    [DisplayAttribute(Name = "Birth Place")]
    public string si_brith_place { get; set; }
    [Required]
    [DisplayAttribute(Name = "Guardian")]
    public string si_g_code { get; set; }
    [Required]
    [DisplayAttribute(Name = "Enrolled")]
    public string si_enrolled { get; set; }
    [Required]
    [DisplayAttribute(Name = "Email")]
    public string si_email { get; set; }
    [Required]
    [DisplayAttribute(Name = "Grade Level")]
    public int si_gl_id { get; set; } //fk
    [Required]
    [DisplayAttribute(Name = "Section")]
    public int si_sec_id { get; set; } //fk

    public IEnumerable<GradeLevel> GradeLevel { get; set; }
    public IEnumerable<Guardian> Guardian { get; set; }
}

public class GradeLevel
{
    public string gl_id { get; set; }
    public string gl_roman_no { get; set; }
    public string gl_name { get; set; }
}


public class Guardian
{
    public string g_ref_code { get; set; }
    public string g_fname { get; set; }
    public string g_mname { get; set; }
    public string g_lname { get; set; }
    public string g_contact { get; set; }
    public string fullName { get; set; }
}

非常感谢帮助:)

2 个答案:

答案 0 :(得分:0)

问题在于:

@Html.DropDownListFor(model => model.si_gl_id, new SelectList(Model.GradeLevel,"gl_id","gl_name"), "Select Grade Level", new { id = "ddlGrade" })

SelectList将Model.GradeLevel作为参数,为null 所以你必须改变这样的代码。

    [HttpPost]
    public ActionResult RegisterStudent(CreateStudent Create)
    {
        acgs_qm.Models.ModelActions Ma = new acgs_qm.Models.ModelActions();
        if (ModelState.IsValid)
        {
            Ma.insertStudent(Create);
        }

        //Populate the values so that they wont be null in selectlist
        Create.GradeLevel = action.getGrade();
        Create.Guardian = action.getGuardian();

        return View(Create);
    }

由于在您的视图中未选择下拉列表,因此模型字段将为null。因此,在post方法中传递给视图时显式分配这些值,因为dropdownlist总是需要该值。 仅当模型传递给视图时,才会设置对象引用。在此代码中,当您将Create传递给视图时,模型对象的引用将设置为Create对象的实例。 这肯定我检查了。只要有Selectlist,您必须确保传递的值不为空。为此目的,我只填充

GradeLevelGuardian

答案 1 :(得分:0)

Http是无国籍的。这意味着它不会在您的2个请求之间保留您的数据。

GET操作方法中,您要加载下拉列表的数据并发送到视图。但是在HttpPost操作方法中,当ModelState.IsValid为false时,您将返回相同的视图。但是,您没有将任何模型/视图模型传递给具有下拉列表数据的模型/视图模型。您的剃刀视图正在尝试使用GradeLevel和Guardian集合来呈现下拉列表,但由于我们没有加载这些属性的数据,因此它是 NULL 。这就是你得到这个错误的原因。

在返回视图之前,您应该做的是再次重新加载GradeLevelGuardian属性值。

[HttpPost]
public ActionResult RegisterStudent(CreateStudent Create)
{
    var Ma = new acgs_qm.Models.ModelActions();
    if (ModelState.IsValid)
    {
        Ma.insertStudent(Create);
    }
    //Reload the data 
    Create.GradeLevel = action.getGrade(),
    Create.Guardian = action.getGuardian(),

    return View(Create);
}