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.

1

2 Answers 2

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));
        }
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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: <%: Html.DropDownListFor(model => model.race, Model.Races, "--Select--")%> by <%: Html.DropDownListFor(model => model.Person.race, Model.Races, "--Select--")%> Thank you soo much!!!
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");
}

2 Comments

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.
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: <%: Html.DropDownListFor(model => model.race, Model.Races, "--Select--")%> by <%: Html.DropDownListFor(model => model.Person.race, Model.Races, "--Select--")%> Thank you soo much!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.