2015-04-19 113 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

相关问题