2011-06-01 86 views
0

在我的應用程序中,我使用ADO實體框架從數據庫填充下拉列表,在此之後,當我嘗試提交表單的下拉列表的值時,它將提供Null引用異常。DropDownListFor在[HttpPost]後的NullReferenceException

出錯代碼(在的Index.aspx)

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

控制器(在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 
     } 

模型(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"); 
     } 

    } 

的驗證模型

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

} 

    public class Person_Validation 
    { 

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

你能幫助我嗎?謝謝。

+1

的[什麼是.NET一個NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2011-06-01 14:37:57

回答

1

在你必須給視圖中的相同類型的模型POST方法。

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

好的,謝謝,我通過返回View(new PersonFormViewModel(person))替換了[HttPost]中的返回值,但變量raze永遠不會被捕獲。競爭變量仍然爲空,並且從未被Person_Validation檢測到。 – Xenetrix 2011-06-01 18:47:14

+0

感謝EduardoCampañó和DanielB。首先,我需要在[HttPost]替換: 返回查看()的返回查看(新PersonFormViewModel(人)) 與從未Person_Validation檢測到的問題只是我需要更換: <%:HTML。 DropDownListFor(model => model.race,Model。種族,「 - 選擇 - 」)%> 由 <%:Html.DropDownListFor(model => ** model.Person.race **,Model.Races,「--Select - 」)% > 謝謝你soo !!! – Xenetrix 2011-06-01 19:10:20

0

您沒有在後置操作中傳遞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)); 
} 

或許

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

好的謝謝你,可能重複我通過返回View(新PersonFormViewModel(person))替換[HttPost]中的返回值,但變量raze永遠不會被捕獲。競爭變量仍然爲空,並且從未被Person_Validation檢測到。 – Xenetrix 2011-06-01 18:47:03

+0

感謝EduardoCampañó和DanielB。首先,我需要在[HttPost]替換: 返回查看()的返回查看(新PersonFormViewModel(人)) 與從未Person_Validation檢測到的問題只是我需要更換: <%:HTML。 DropDownListFor(model => model.race,Model.Races,「--Select - 」)%> by <%:Html.DropDownListFor(model => ** model.Person.race **,Model。種族,「 - 選擇 - 」)%> 謝謝你soo !!! – Xenetrix 2011-06-01 19:10:10

相關問題