简体   繁体   English

[HttpPost] 之后 DropDownListFor 上的 NullReferenceException

[英]NullReferenceException on DropDownListFor after [HttpPost]

In my application I'am populating a dropdownlist from database using ADO Entity Framework, after this when i try to submit the form the value of the dropdown list it is giving Null reference exception.在我的应用程序中,我正在使用 ADO 实体框架从数据库中填充一个下拉列表,之后当我尝试提交下拉列表的值时,它给出了 Null 引用异常。

Error Code (in INDEX.ASPX)错误代码(在 INDEX.ASPX 中)

<%: Html.DropDownListFor(model => model.race, Model.Races, "--Select--")%>  <--error
<%: Html.ValidationMessageFor(model => model.race)%>

CONTROLLER (in NewPersonController) CONTROLLER(在 NewPersonController 中)

public ActionResult Index()
        {
            Person person= new Person();
            return View(new PersonFormViewModel(person));   
        }


[HttpPost]
public ActionResult Index(Person person)
        {
            if (ModelState.IsValid) //Also not enter the race parameter
            {
                personRepo.Add(person);
                personRepo.Save();
            }
            return View();  // When return the view I get the error
        }

MODEL (in PersonFormViewModel) MODEL(在 PersonFormViewModel 中)

public class PersonFormViewModel
    {


        public Person Person        {
            get;
            private set;
        }

        public SelectList Races
        {
            get;
            private set;
        }

        public string race
        {
            get { return Person.race; }
            set { Person.race = value; }
        }


        public PersonFormViewModel(Person person)
        {

            Person = person;
            RaceRepository repository = new RaceRepository();

            IList<Race> racesList= repository.FindRaces().ToList();
            IEnumerable<SelectListItem> selectList =
                from c in racesList
                select new SelectListItem
                {
                    Text = c.race,
                    Value = c.id.ToString()
                };

            Races = new SelectList(selectList, "Value", "Text");   
        }

    }

IN VALIDATION MODEL验证中 MODEL

[MetadataType(typeof(Person_Validation))]
public partial class Person    {

}

    public class Person_Validation
    {

        [Required(ErrorMessage = "Required race")]
        public string race
        {
            get;
            set;
        }
    }

Can you help me please?你能帮我吗? Thank you.谢谢你。

In the POST method you have to give the same type of model to the view.在 POST 方法中,您必须将相同类型的 model 提供给视图。

        [HttpPost]
        public ActionResult Index(Person person)
        {
            if (ModelState.IsValid)
            {
                personRepo.Add(person);
                personRepo.Save();
            }
            return View(new PersonFormViewModel(person));
        }

You're not passing the ViewModel in the post action.您没有在 post 操作中传递 ViewModel。

[HttpPost]
public ActionResult Index(Person person)
{
    if (ModelState.IsValid) //Also not enter the race parameter
    {
        personRepo.Add(person);
        personRepo.Save();
    }
    return View(new PersonFormViewModel(person));
}

Or maybe或者可能

[HttpPost]
public ActionResult Index(Person person)
{
    if (ModelState.IsValid) //Also not enter the race parameter
    {
        personRepo.Add(person);
        personRepo.Save();
    }
    return RedirectToAction("Index");
}

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

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