0

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.

Error Code (in INDEX.ASPX)

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

CONTROLLER (in 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)

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

[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.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Xenetrix
  • 177
  • 1
  • 17
  • 1
    possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Jun 01 '11 at 14:37

2 Answers2

1

In the POST method you have to give the same type of model to the view.

        [HttpPost]
        public ActionResult Index(Person person)
        {
            if (ModelState.IsValid)
            {
                personRepo.Add(person);
                personRepo.Save();
            }
            return View(new PersonFormViewModel(person));
        }
DanielB
  • 18,800
  • 2
  • 41
  • 49
  • Ok Thank you, I replace the return in [HttPost] by return View(new PersonFormViewModel(person)), but the variable raze is never catched. the race variable still null and never detected by Person_Validation. – Xenetrix Jun 01 '11 at 18:47
  • Thanks to Eduardo Campañó and DanielB. First I need to replace in [HttPost]: return View() by return View(new PersonFormViewModel(person)) and the problem with never detected by Person_Validation just I need to replace: model.race, Model.Races, "--Select--")%> by **model.Person.race**, Model.Races, "--Select--")%> Thank you soo much!!! – Xenetrix Jun 01 '11 at 19:10
0

You're not passing the ViewModel in the post action.

[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");
}
Eduardo Campañó
  • 6,760
  • 4
  • 25
  • 24
  • Ok thank you, I replace the return in [HttPost] by return View(new PersonFormViewModel(person)), but the the variable raze is never catched. the race variable still null and never detected by Person_Validation. – Xenetrix Jun 01 '11 at 18:47
  • Thanks to Eduardo Campañó and DanielB. First I need to replace in [HttPost]: return View() by return View(new PersonFormViewModel(person)) and the problem with never detected by Person_Validation just I need to replace: model.race, Model.Races, "--Select--")%> by **model.Person.race**, Model.Races, "--Select--")%> Thank you soo much!!! – Xenetrix Jun 01 '11 at 19:10