任何人都可以幫助我編程 Asp.Net.Core MVC 的初學者我在 HomeController.cs 中有這兩種方法并嘗試從資料庫 FactoryContext 表 MoldingOrders 進行查詢并將視圖特定列添加到 PartialView。我試圖在網上找到一些解決方案,但沒有任何幫助。
public ActionResult Index()
{ return View(); }
public PartialViewResult Backlog()
{
try {
var contract = (from auftrag in FactoryContext.MoldingOrders
select auftrag);
contract.ToListAsync();
ViewData["ContractList"] = contract;
return PartialView("~/Views/Shared/_Backlog.cshtml");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
這是模型 MoldingOrders.cs,它是現有資料庫的腳手架
public partial class MoldingOrders
{
public MoldingOrders()
{
MoldingOrdersPlan = new HashSet<MoldingOrdersPlan>();
}
public int Id { get; set; }
public string ExportDate { get; set; }
public string Auftrag { get; set; }
public string Material { get; set; }
public string Materialkurztext { get; set; }
public string Fertsteu { get; set; }
public string Disponent { get; set; }
public string Eckende { get; set; }
public int? Sollmenge { get; set; }
public string Materialfixed { get; set; }
public virtual ICollection<MoldingOrdersPlan> MoldingOrdersPlan { get; set; }
部分視圖 - _Backlog.cshtml
@using ProductionPlanningApp.Models;
@model IEnumerable<MoldingOrders>
@{
List<MoldingOrders> ContractList = (List<MoldingOrders>)ViewData["EmployeesList"];
}
<html>
<body>
<div class="overflow-auto position-absolute position-table">
<table class="table table-bordered w-auto">
<thead class="table-dark">
<tr>
<th class="text-md-center">BACKLOG</th>
</tr>
</thead>
<tbody>
@foreach(var item in ViewData["contracts"] as IEnumerable<ProductionPlanningApp.Models.MoldingOrders>)
{
<tr class="d-flex">
<td>
<button type="button"class="btn btn-outline-dark btn-link btn-sm">@item.Auftrag</button>
</td>
</tr>
}
</tbody>
</table>
</div>
</body>
</html>
代碼將運行,但加載后會拋出 System.NullReferenceException

uj5u.com熱心網友回復:
您應該將合同更改為 ContractList。
@foreach(var item in ViewData["ContractList"] as IEnumerable<ProductionPlanningApp.Models.MoldingOrders>)
uj5u.com熱心網友回復:
嘗試設定ViewData["contracts"]您的操作:
public PartialViewResult Backlog()
{
try {
var contract = (from auftrag in FactoryContext.MoldingOrders
select auftrag);
contract.ToListAsync();
ViewData["contracts"] = contract;
return PartialView("~/Views/Shared/_Backlog.cshtml");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
uj5u.com熱心網友回復:
也許我弄錯了,但是在您的第一個代碼塊中,您使用“ContractList”作為 ViewData 的鍵,但在最后一個代碼塊中,您嘗試訪問“合同”。
對不起,如果我弄錯了,這讓我想知道。
編輯:如果兩者都應包含相同的資料,則鍵應匹配。嘗試將關鍵“合同”更改為“合同串列”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/493139.html
標籤:C# asp.net-mvc 林克 asp.net 核心
