2015-04-19 40 views
0

我想創建一個索引頁(其產品列表使用IEnumerable)的組合視圖和創建頁面(其中有添加/保存的東西) ,我收到了lambda表達式的錯誤。asp.Net中的組合視圖和IEnumerable模型的問題MVC

這裏是我的代碼:

@model IEnumerable<OIS.Models.Category> 

<table class="table"> 
<thead> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.category_name) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.date_created) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.date_updated) 
     </th> 
     <th></th> 
    </tr> 
</thead> 
<tbody> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.category_name) 

      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.date_created) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.date_updated) 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id = item.ID }) | 
       @Html.ActionLink("Details", "Details", new { id = item.ID }) | 
       @Html.ActionLink("Delete", "Delete", new { id = item.ID }) 
      </td> 
     </tr> 
    } 
</tbody> 
</table> 

//For for Creating new Item 
@using (Html.BeginForm()){ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Category</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     //Im Gettig Error with this line (model => model.category_name) 
     @Html.LabelFor(model => model.category_name, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      //Im Gettig Error with this line (model => model.category_name) 
      @Html.EditorFor(model => model.category_name, new { htmlAttributes = new { @class = "form-control" } }) 
      //Im Gettig Error with this line (model => model.category_name) 
      @Html.ValidationMessageFor(model => model.category_name, "", 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> 
}  

我該怎麼辦? 編輯: 這裏是我的控制器

namespace OIS.Controllers{ 
public class CategoryController : Controller 
{ 
    private DbOnlineIS db = new DbOnlineIS(); 

    // GET: Category 
    public ActionResult Index() 
    { 
     return View(db.Categories.ToList()); 
    } 

    // Post: Category 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Index([Bind(Include = "ID,category_name,date_created,date_updated")] Category category) 
    { 
     if (ModelState.IsValid) 
     { 
      category.date_created = DateTime.Now; 
      category.date_updated = DateTime.Now; 
      db.Categories.Add(category); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(category); 
    } 
    // GET: Category/Create 

    public ActionResult Create() 
    { 
     return View(); 
    } 

    // POST: Category/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([Bind(Include = "ID,category_name,date_created,date_updated")] Category category) 
    { 
     if (ModelState.IsValid) 
     { 
      category.date_created = DateTime.Now; 
      category.date_updated = DateTime.Now; 
      db.Categories.Add(category); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(category); 
    } 
+0

什麼錯誤正在發生? –

+0

您必須創建ViewModel –

+0

錯誤說'不包含category_name的定義' – jessemiel

回答

4

要製作組合視圖您必須執行下面的操作。

創建具有兩個屬性

public class CategoryViewModel { 
    public IEnumerable<OIS.Models.Category> Categories { get; set; } 
    public Category Category { get; set; } 
} 

之後,你移動,並創建一個視圖模型列表,從2個局部視圖查看。比方說_CategoryCreatePartial.cshtml_CategoryListPartial.cshtml

比起組合視圖成爲這樣的事情,Index.cshtml

@model OIS.Models.CategoryViewModel 

@Html.Partial("_CategoryListPartial", Model.Categories) 

@Html.Partial("_CategoryCreatePartial", Model.Category) 

局部視圖:

_CategoryListPartial.cshtml

@model IEnumerable<OIS.Models.Category> 

<table class="table"> 
<thead> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.category_name) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.date_created) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.date_updated) 
     </th> 
     <th></th> 
    </tr> 
</thead> 
<tbody> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.category_name) 

      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.date_created) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.date_updated) 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id = item.ID }) | 
       @Html.ActionLink("Details", "Details", new { id = item.ID }) | 
       @Html.ActionLink("Delete", "Delete", new { id = item.ID }) 
      </td> 
     </tr> 
    } 
</tbody> 
</table> 

_CategoryCreatePartial.cshtml

@model OIS.Models.Category 

@using (Html.BeginForm("Create", "Category")){ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Category</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      //Im Gettig Error with this line (model => model.category_name) 
      @Html.LabelFor(model => model.category_name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       //Im Gettig Error with this line (model => model.category_name) 
       @Html.EditorFor(model => model.category_name, new { htmlAttributes = new { @class = "form-control" } }) 
       //Im Gettig Error with this line (model => model.category_name) 
       @Html.ValidationMessageFor(model => model.category_name, "", 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> 
}  

控制器和索引操作

public class CategoryController : Controller 
{ 
    private DbOnlineIS db = new DbOnlineIS(); 

    public ActionResult Index() { 
     var categoryViewModel = new CategoryViewModel(); 
     categoryViewModel.Categories = db.Categories.ToList(); 
     categoryViewModel.Category = new Category(); 
     return View(categoryViewModel); // list + create category 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Category category) // call this action to create category. 
    { 
     if (ModelState.IsValid) 
     { 
      category.date_created = DateTime.Now; 
      category.date_updated = DateTime.Now; 
      db.Categories.Add(category); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(category); 
    } 
} 
+0

嘿!我嘗試了你的答案,看起來不錯,但是我在這個部分發現了一些錯誤.. '@ Html.Partial(「_ CategoryListPartial」,CategoryViewModel.Categories)' '@ Html.Partial(「_ CategoryCreatePartial」,CategoryViewModel。類別)'錯誤說「名稱CategoryViewModel不存在於當前上下文中」 – jessemiel

+0

我明白了!我剛剛更改了 '@ Html.Partial(「_ CategoryListPartial」,CategoryViewModel.Categories)' into '@ Html.Partial(「_ CategoryListPartial」,Model.Categories)' 非常感謝您的幫助! – jessemiel

0

你的問題是在<thead>。您嘗試從類型模型訪問屬性:

@Html.DisplayNameFor(model => model.category_name) 

但你的模型是一個IEnumerable和沒有財產category_name

1

您需要創建另一個視圖模型,以滿足顯示列表的這一要求以及創建屏幕。

public class CategoryViewModel 
{ 
    public List<OIS.Models.Category> Categories {get;set;} 
    public OIS.Models.Category Category {get;set;} 
} 

在您的Razor視圖模式設置爲這個新的視圖模型

@model CategoryViewModel 

在你的枚舉,你現在從這個新的視圖模型

​​

內訪問列表中爲您創建部分訪問來自ViewModel內的類別

@Html.LabelFor(model => model.Category.category_name, htmlAttributes: new { @class = "control-label col-md-2" })  

在相同的行上,您需要重構View頁面。

在您的控制器中,您目前必須傳遞一個List。你將需要改變,以通過你的新CategoryViewModel

List<Category> categories = "your db call"; 
CategoryViewModel viewModel = new CategoryViewModel(); 
viewModel.Categories = categories; 
viewModel.Category = new Category(); 
return View(viewModel); 
+0

嗨!我嘗試了你的建議,而且它很棒。但現在我得到這個錯誤消息.. 傳遞到字典中的模型項是類型'System.Collections.Generic.List'1 [OIS.Models.Category]',但是這個字典需要一個'OIS'類型的模型項.Models.CategoryViewModel」。 ...那是什麼意思? – jessemiel

+0

@jesse在你的控制器代碼中,你正在傳遞一個類別列表。你需要改變它並傳遞CategoryViewModel。更新了答案以反映這一點。 –

+0

我很抱歉,我真的是一個noob這裏。,我應該把什麼「你的數據庫調用」部分? – jessemiel

相關問題