如何通过IEnumerable列表进行查看

时间:2019-02-12 09:36:52

标签: c# asp.net-mvc

再次,我想问一些有关如何制作和传递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>
}

1 个答案:

答案 0 :(得分:3)

您的模型看起来很像存储库或服务。通常,模型不参与数据库或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)
{ 
   ...
}