使用ASP.NET MVC,.NET Framework 4.5.2,SQL DB的实体数据模型,Visual Studio 2017。
我有一个从ADO.NET生成的类(来自数据库的EF Designer):
BookInfo.cs
namespace LibraryMS
{
using System;
using System.Collections.Generic;
public partial class BookInfo
{
public string BookID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Publisher { get; set; }
public string PublishDate { get; set; }
public string Edition { get; set; }
public virtual Inventory Inventory { get; set; }
}
}
数据库设计在" BookID"在BookInfo表中有一个外键" BookID"在库存表中。 为了更新" BookID"引用的广告资源属性,我继续查询列表并更新正确的实例。
更新广告资源页面的屏幕截图:
当登陆页面输入信息时,点击"创建"时会调用[HttpGet] UpdateInventory()。按钮如上图所示,调用[HttpPost] UpdateInventory(...)。
控制器中的逻辑/代码:
[HttpGet]
public ActionResult UpdateInventory()
{
return View();
}
[HttpPost]
public async Task<ActionResult> UpdateInventory(string bookID, string ttlIn, string lowin, string outnow)
{
var bf = await SqlRestApiHelper.searchFromBooks(bookID);
bf.Inventory.TotalIn = Convert.ToInt16(ttlIn);
bf.Inventory.LowIn = Convert.ToInt16(lowin);
bf.Inventory.Out = Convert.ToInt16(outnow);
await SqlRestApiHelper.UpdateBookInfoInventory(bf.Inventory);
await SqlRestApiHelper.SaveChanges();
return View("All");
}
[HttpGet]
public async Task<ActionResult> All()
{
return View(await SqlRestApiHelper.getAllBooksInfo(0, 10));
}
SqlRestApiHelper.cs
namespace LibraryMS
{
public static class SqlRestApiHelper
{
private static libraryDBEntities entities = new libraryDBEntities();
public static async Task<LibraryMS.BookInfo> searchFromBooks(string id)
{
return entities.BookInfoes.ToList().Find(book => book.BookID == id);
}
public static async Task UpdateBookInfoInventory(LibraryMS.Inventory inv)
{
var newInv = inv;
var el = entities.BookInfoes.ToList().Find(x => x.Inventory.BookID == newInv.BookID);
if (el != null)
{
el.Inventory.TotalIn = newInv.TotalIn;
el.Inventory.LowIn = newInv.LowIn;
el.Inventory.Out = newInv.Out;
// the above updates the list item referenced
}
}
public static async Task SaveChanges()
{
await entities.SaveChangesAsync();
}
public static async Task<IPagedList<BookInfo>> getAllBooksInfo(int page, int itemsPerPage)
{
List<BookInfo> bookinfo = new List<BookInfo>();
bookinfo = (from o in entities.BookInfoes
orderby o.Title descending //use orderby, otherwise Skip will throw an error
select o)
.Skip(itemsPerPage * page).Take(itemsPerPage)
.ToList();
int totalCount = bookinfo.Count();//return the number of pages
IPagedList<BookInfo> pagebooks = new StaticPagedList<BookInfo>(bookinfo, page + 1,10,totalCount);
return pagebooks;//the query is now already executed, it is a subset of all the orders.
}
抛出空的异常:
all.cshtml视图页面的代码:
@model PagedList.IPagedList<LibraryMS.BookInfo>
@using PagedList.Mvc;
@{
ViewBag.Title = "All";
}
<h2>all</h2>
<table class="table">
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
</td>
<td>
@Html.DisplayFor(modelItem => item.PublishDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Edition)
</td>
<td>
@Html.ActionLink("Details","Details",new { item.BookID})
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page => Url.Action("All","BookInfoController", new { page }))
答案 0 :(得分:0)
您的视图引发错误,因为您在不传递模型的情况下返回视图,而是在不传递模型的情况下使用return View("All")
正确的方法是通过视图传递模型,你可以这样做
View("ViewName", ModelData);
在你的情况下
return View("All", await SqlRestApiHelper.getAllBooksInfo(0, 10));
对于保存部分我不知道为什么,但我可以看到很少的错误, 首先,如果书没有库存信息怎么办? 它将抛出null错误,因此首先检查是否为null,创建新库存,如果不相应更新 这是我将如何做到这一点
public static async Task UpdateBookInfoInventory(Inventory inv)
{
var newInv = inv;
// get book info
var el = entities.BookInfoes.FirstOrDefault(x => x.BookID == inv.BookID);
if (el != null)
{
if(el.Inventory != null)
{
// update accordingly
el.Inventory.TotalIn = newInv.TotalIn;
el.Inventory.LowIn = newInv.LowIn;
el.Inventory.Out = newInv.Out;
// the above updates the list item referenced
}
else
{
/// add new if null
el.inventory = newInv;
}
await SqlRestApiHelper.SaveChanges();
}
答案 1 :(得分:0)
作为第一步,将断点放在getAllBooksInfo方法中,查看Visual Studio中是否有列表计数。这对你有很大帮助。
作为替代步骤,您还可以使用ToPagedList(pageIndex,pageSize)来解决此错误;方法,我个人使用它,它运作良好
根据您的代码ToPagedList示例:
**public static async Task<IPagedList<BookInfo>> getAllBooksInfo(int page, int itemsPerPage)
{
List<BookInfo> bookinfo = new List<BookInfo>();
bookinfo = (from o in entities.BookInfoes
orderby o.Title descending //use orderby, otherwise Skip will throw an error
select o)
.Skip(itemsPerPage * page).Take(itemsPerPage)
.ToList();
int totalCount = bookinfo.Count();//return the number of pages
//changes made to the below line
IPagedList<BookInfo> pagebooks = bookinfo.ToPagedList(1, 10);
return pagebooks;//the query is now already executed, it is a subset of all the orders.
}**
官方来源: https://github.com/troygoode/PagedList
注意:即使使用此方法,也请使用更改行中的断点检查是否从数据库获取数据。
希望这肯定会解决您的问题,请让我知道想法或反馈
由于
KARTHIK