0

我正在嘗試爲類別創建子類別。用戶首先從下拉列表中選擇類別,然後鍵入子類別名稱並單擊提交。即使下拉列表元素正確填寫下拉列表。當我點擊提交按鈕它會創建錯誤。我該如何解決這個問題? 我的觀點:dropdownlistfor yield yield {{「對象引用未設置爲對象的實例。」},當我單擊提交按鈕時

@model CETAPPSUGG.Models.CategorySubCategoryModel 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.HiddenFor(model => model.selectedId, new { id = "3" }); 


    // @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>SubCatagories</h4> 

     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.SubCategory.SubCategoryName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.SubCategory.SubCategoryName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.SubCategory.SubCategoryName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
Upper cat:   <div class="col-md-10"> 
          @Html.DropDownListFor(Model => Model.Categories, Model.categoryList) 


      </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> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

我的控制器:

public ActionResult Create() 
    { 
     var categories = db.Categories.ToList(); 
     CategorySubCategoryModel deneme = new CategorySubCategoryModel(); 
     var list = new List<SelectListItem>(); 
     deneme.Categories = categories; 
     foreach (Categories c in categories) 
     { 
      list.Add(new SelectListItem() { Text = c.CategoryName, Value = c.Id.ToString() }); 
     } 

     deneme.categoryList = list; 

     return View(deneme); 
    } 


    // POST: SubCatagories/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    // [ValidateAntiForgeryToken] 
public ActionResult Create(CategorySubCategoryModel model) 
{ 
    string strDDLValue = model.selectedId; 
    SubCatagories newSubCategory = new SubCatagories(); 
    Categories cat = new Categories(); 
    cat = db.Categories.Find(Convert.ToInt32(strDDLValue)); 
    // cat = db.Categories.Find(Convert.ToInt32(strDDLValue)); 

    newSubCategory.SubCategoryName = model.SubCategory.SubCategoryName; 
    newSubCategory.UpperCategory = Convert.ToInt32(strDDLValue); 
    newSubCategory.Categories = cat; 

    db.SubCatagories.Add(newSubCategory); 
    db.SaveChanges(); 

    return View(); 
} 

我的模型

namespace CETAPPSUGG.Models 
{ 
    public class CategorySubCategoryModel 
    { 
     SubCatagories SubCatagories { get; set; } 

     public IEnumerable<Categories> Categories { get; set; } 
     public IEnumerable<SubCatagories> SubCategories { get; set; } 

     public IEnumerable<SelectListItem> categoryList { get; set; } 

     public SubCatagories SubCategory { get; set; } 

     public string selectedId; 

    } 
} 

它鑑於

+1

可能重複[什麼是NullReferenceException,以及如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix -it) – mmushtaq

回答

0

產生錯誤你有一大堆的問題在這裏。

您的主要問題是您沒有將模型傳回給後期視圖,因此該模型爲空。因此,當您嘗試在視圖中從模型中取消引用項目時,會生成空引用。

首先,您使用的是selectedId,但不要將其設置在任何位置。它不會被魔法設定。你可能想要的是@Html.DropDownListFor(model => model.selectedId, Model.categoryList)(注意第一個參數中模型中的小寫字母m,第二個參數中的大寫字母M)

其次,不要在您的lambda中使用模型DropDownListFor,請使用小寫模型,因爲大寫模型是爲實際模型實例保留的。如果你想引用模型實例,那麼做一些像DropDownListFor(_ => Model.Foo, Model.Foos)。請注意,我在lambda之前使用下劃線或其他非Model的其他值替換了模型。坦率地說,我很驚訝,這甚至可以起作用,但是這裏可能有一個覆蓋外部模型的範圍規則。避免這種情況,因爲它可能會導致你在路上感到困惑。

第三,您將IEnumerable傳遞給DropDownListFor作爲選定的項目變量,這不適用於多個級別。在大多數情況下,這需要是單個字符串值(有時是數字字符串,但總是可以調用ToString()並獲得合理的字符串,因爲DropDownListFor無法顯示覆雜的對象)。

第四,您還需要在Post操作中重新填充DropDownListFor,因爲下拉列表的內容未回傳,因此在模型中將爲空。這與您的視圖中的子類別derefences一起最終生成了Null引用異常。

您還需要將模型傳遞迴Post中的視圖,但是如上所述,它需要使用Categories以及SubCategories重新初始化。

這裏可能有更多的問題,但解決這些問題,你應該在路上。

+0

非常感謝。我認爲我的代碼是垃圾。但我會盡力修復它。但是我改變了@ Html.DropDownListFor(model => model.selectedId,Model.categoryList),但它給了錯誤。我應該做什麼改變? – user2394800

+0

return RedirectToAction(「Index」); 我加了這個。它現在有效。但我無法訪問selectedId呢。 ıt爲空。爲什麼? – user2394800

+0

@ user2394800 - 我給你4件你必須改變的東西,你改變了其中一個,並說:「它仍然不起作用!「試試我所說的一切, –

相關問題