Can anyone help me I beginner in programming Asp.Net.Core MVC I have this two methods in HomeController.cs and trying to make a query from database FactoryContext table MoldingOrders and make a view specific column to PartialView. I was trying to find some solution on web but nothing help enough.
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;
}
}
This is the model MoldingOrders.cs it was scaffold from existing database
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; }
Partial View - _Backlog.cshtml
@using ProductionPlanningApp.Models;
@model IEnumerable<MoldingOrders>
@{
List<MoldingOrders> ContractList = (List<MoldingOrders>)ViewData["EmployeesList"];
}
<html>
<body>
<div >
<table >
<thead >
<tr>
<th >BACKLOG</th>
</tr>
</thead>
<tbody>
@foreach(var item in ViewData["contracts"] as IEnumerable<ProductionPlanningApp.Models.MoldingOrders>)
{
<tr >
<td>
<button type="button">@item.Auftrag</button>
</td>
</tr>
}
</tbody>
</table>
</div>
</body>
</html>
Code will run but after loading will trow System.NullReferenceException
CodePudding user response:
Perhaps I'm mistaking, but in your first codeblock you use "ContractList" as key for ViewData, but in the last codeblock you try to access "contracts". Sorry if I'm mistaking, this was something which made me wonder.
CodePudding user response:
You should change contracts to ContractList.
@foreach(var item in ViewData["ContractList"] as IEnumerable<ProductionPlanningApp.Models.MoldingOrders>)