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



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

非常感谢您的帮助:)

问题就在这里:

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

SelectList以Model.GradeLevel为参数,为空所以你必须这样修改代码。

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

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

CCD_ 7和CCD_ 8

Http是无状态的。这意味着它不会在您的两个请求之间保留您的数据。

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

相关内容

最新更新