c# - 如何将 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>
}
解决方案
您的模型看起来很像存储库或服务。通常,模型不参与数据库或 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)
{
...
}
推荐阅读
- flutter - 如何根据使用 Flutter/Dart 的选择将单个 Firestore 文档 ID 传递给另一个类?
- javascript - 如果可滚动元素溢出,则 scrollIntoView 在 Firefox Android 中不起作用:隐藏
- google-coral - 通过串行控制台连接到开发板时如何禁用登录提示?
- amazon-web-services - 将子域指向 EC2 实例?
- python - “激活”类型的对象没有 len()
- python - 如何访问 Selenium 中的第二个 div 节点(Python)
- python - 选择总和以满足生产输出的最小机器数量 - 简化代码?
- python - 在大型二进制文件中重新排序行的最佳方法是什么?
- python - 导入 ipinfo - 不工作 - 无法导入名称抽象方法
- java - Maven & Eclipse 远程调试:连接被拒绝