首页 > 解决方案 > 如何将 IEnumerable 列表传递给查看

问题描述

我想再问一些关于如何制作和传递 IEnumerable 列表的问题?

我的控制器:

public class BookController : Controller
{
   // GET: Book
    public ActionResult Index()
    {
        BookModel viewBookList = new BookModel();
        viewBookList.ViewBookListing(); // viewBookList.ViewBookListing() 
                                        // pass the model values in 
                                        // ViewBookListing method which is located into my bookmodel
        return View(viewBookList);
    }
}

我的模型:

  namespace myWeb.Models
  {
        public class BookModel
        {
            public List<BookModel> showAllBooks = new List<BookModel>();
            // *other properties*
 public DataTable viewAllBooks()
        {
            try
            {
                Connection viewAll = new Connection();
                viewAll.cmdType = CommandType.StoredProcedure;
                viewAll.SQL = "insertSelectUpdateDeleteBooks";
                viewAll.ParamAndValue = new string[,]
                {
                {"@bookid", "" },
                {"@authorid", "" },
                {"@bookcatid", "" },
                {"@booktitle", ""},
                {"@isbn", ""},
                {"@pubplace", "" },
                {"@pubdate", "" },
                {"@bookphoto", "" },
                {"@statementtype", "Select" }
                };
                return viewAll.GetData();
            }
            catch (Exception)
            {
                throw;
            }
   }

返回 viewAll.GetData(); 这个 GetData 从我的数据访问层获取数据或记录

        public void ViewBookListing()
        {
            List<BookModel> showAllBooks = new List<BookModel>();
            DataTable dt = this.viewAllBooks();
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        showAllBooks.Add(new BookModel()
                        {
                            qty = int.Parse(row["Quantity"].ToString()),
                            bookTittle = row["bookTitle"].ToString(),
                            authorName = row["Author"].ToString(),
                            ISBN = row["ISBN"].ToString(),
                            pubPlace = row["pubPlace"].ToString(),
                            pubDate = DateTime.Parse(row["pubDate"].ToString()),
                            DDC = row["DDC"].ToString(),
                            edition = row["edition"].ToString(),
                            volume = row["volume"].ToString(),
                            CategoryName = row["Category"].ToString()

                        });
                    }
                }
            }
            this.showAllBooks = showAllBooks;
        }
    }
}

这是我的视图页面或索引。我不喜欢这种将数据传递到视图中的方式,我想传递模型数据或使用的数据,IEnumerable但问题是,我不知道该怎么做。

@using myWeb.Models;
@model BookModel
@{
    ViewBag.Title = "Index";
}

@foreach(myWeb.Models.BookModel item in Model.showAllBooks)
{
        <tr>
            <td></td>
            <td>@Html.DisplayFor(Model => item.bookTittle)</td>
            <td>@Html.DisplayFor(Model => item.authorName)</td>
            <td>@Html.DisplayFor(Model => item.ISBN)</td>
            <td>@Html.DisplayFor(Model => item.CategoryName)</td>
            <td>@Html.DisplayFor(Model => item.DDC)</td>
            <td>@Html.DisplayFor(Model => item.pubPlace)</td>
            <td>@Html.DisplayFor(Model => item.pubDate)</td>
            <td></td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
}

标签: c#asp.net-mvc

解决方案


您的模型看起来很像存储库或服务。通常,模型不参与数据库或 UI 活动。

但要回答你的问题,

    public ActionResult Index()
    {
        BookModel viewBookList = new BookModel();
        viewBookList.ViewBookListing(); //this should be done by a Service
        return View(viewBookList.showAllBooks);
    }

@using myWeb.Models;
@model List<BookModel>
@{
    ViewBag.Title = "Index";
}

@foreach(var item in Model)
{ 
   ...
}

推荐阅读