2017-07-26 73 views
0

我是ASP.NET MVC的新手,我正在開發我的第一個項目,只是爲了好玩。 我得到這個ArgumentNullException,我無法弄清楚什麼是錯的。C#razorview DropDownListFor'值不能爲空'

這是我的模型:

public class SpeciesLabel 
{ 
    [Key] 
    [Required] 
    public string Name { get; set; } 

    [Required] 
    public CustomGroup CustomGroup { get; set; } 

    [Required] 
    public Family Family { get; set; } 

    [Required] 
    public Genus Genus { get; set; } 

    [Required] 
    public Species Species { get; set; } 
} 

public class SpeciesLabelDbContext : DbContext 
{ 
    public SpeciesLabelDbContext() 
     : base("DefaultConnection") 
    { 

    } 

    public DbSet<SpeciesLabel> SpeciesLabel { get; set; } 
} 

這是控制器:

public ActionResult Create() 
    { 
     List<SelectListItem> customGroups = new List<SelectListItem>(); 
     IQueryable<string> customGroupsQuery = from g in customGroupsDb.CustomGroup 
      select g.Name; 

     foreach (var element in customGroupsQuery) 
     { 
      customGroups.Add(new SelectListItem() 
      { 
       Value = element, 
       Text = element 
      }); 
     } 

     ViewBag.CustomGroup = customGroups; 

這是控制器POST請求:

public ActionResult Create([Bind(Include = "CustomGroup,Family,Genus,Species")] SpeciesLabel speciesLabel) 
    { 
     if (ModelState.IsValid) 
     { 
      db.SpeciesLabel.Add(speciesLabel); 
      db.SaveChanges(); 
      return RedirectToAction("Create"); 
     } 

     return View(); 
    } 

而且本所認爲:

<pre> 
     @model PlantM.Models.PlantModels.SpeciesLabel 

@{ 
    ViewBag.Title = "Create"; 
} 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Species label</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.CustomGroup, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownListFor(model => model.CustomGroup, new SelectList(ViewBag.CustomGroupList, "Value", "Text"), "Please select...", new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.CustomGroup, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 
</pre> 

我有視圖中的所有屬性的輸入,但是我剪切它們,因爲它們與此類似,異常情況相同。只有屬性名稱不會從視圖中返回,因爲它將在控制器中進行設計(其他屬性的串聯)。

這是例外,我是說我提交表單:

ArgumentNullException

編輯:

加入ViewBag初始化的POST創建方法與ArgumentNullException該問題後解決,但我仍然收到空值參數,並且由於這個原因無法創建對象,並且創建視圖被一次又一次地調用!?任何人都可以建議爲什麼這些@ Html.DropDownListFor不會發布任何價值的控制器?

+0

你是說'ViewBag.CustomGroupList'是NULL? – pookie

+0

嗯,我認爲這不是空,因爲我看到下拉菜單中的列表,但是當我選擇一個選項,然後單擊提交按鈕時,我得到此異常。我想將數據發送回控制器存在問題,但我不知道有什麼問題。 –

+0

將[HttpPost]放在創建動作之上? – pookie

回答

0

從評論中,它聽起來像您在第一次訪問時看到的視圖,但發佈後會發生空異常。

如果上述假設是正確的,那麼我覺得你的問題是因爲當你回來後,你的模型沒有通過驗證(例如,也許需要輸入字段沒有張貼回值),這意味着ModelState.IsValid是假的,所以return View()被稱爲

這是問題所在,你是不是在之前回報設定ViewBag.CustomGroup = customGroups;,因此ViewBag.CustomGroup爲空,這就是爲什麼你看到的例外。

初始化ViewBag就像你是如何做到這一點,那麼你應該能夠看到該頁面。

+0

謝謝,這工作!可能狀態是無效的因爲我沒有返回所需的屬性之一,因爲我想在控制器中創建它作爲一些返回屬性的連接。可能我應該重新考慮這一點。再一次感謝你! –

+0

我仍然遇到無法解決的問題。你能看到我的帖子上面的編輯?謝謝! –

+0

@MarianJordanov最好是創建另一個更好的可見性問題 - 但你必須提供更多的描述。 如哪行發生錯誤?它發生在獲得或發佈?如果你描述問題 更詳細我將很樂意提供一些答案 –