[asp.net-mvc]组合的视图和 IEnumerable asp.Net MVC 中的模型问题

发布时间: 2016/10/7 2:36:11
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图创建联合视图的索引页 (其中有产品列表使用 IEnumerable ) 和具有创建页面 (其中已加载 / 保存的东西),我开始使用 lambda 表达式错误。

这里是我的代码︰

@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);
    }

解决方法 1:

使联合视图你不得不做某事喜欢下面。

创建具有 2 属性 ViewModel

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);
    }
}
官方微信
官方QQ群
31647020