I am trying to create a asp.net core 2.1 razor website that will load two tables on the one page. Below i have defined my classes ItemMasters and DealMasters. ItemMasters stores items of clothing and DealMasters stores deals on them items. DealMasters stores the relevant ItemID with DealItemId
im trying to have the getItem Page to show the deals table but only the entires where the DealItemId matches the ID of the item on the page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Mvc;
namespace demo.Data
{
public class ItemsMasters
{
[Key]
[Display(Name = "Itemid")]
public int ItemID { get; set; }
[Required(ErrorMessage = "Please enter this items Name")]
[Display(Name = "ItemName")]
public string ItemName { get; set; }
[Required(ErrorMessage = "Please enter this items Description")]
[Display(Name = "ItemDesc")]
public string ItemDesc { get; set; }
[Required(ErrorMessage = "Please enter this items Colour")]
[Display(Name = "ItemColour")]
public string ItemColour { get; set; }
[Required(ErrorMessage = "Please enter this items Catergory")]
[Display(Name = "ItemCatergory")]
public string ItemCatergory { get; set; }
[Display(Name = "ItemImage")]
public string ItemImage { get; set; }
[NotMapped]
[Display(Name = "Image")]
public IFormFile Image { get; set; }
[Display(Name = "ItemBrand")]
public string ItemBrand { get; set; }
[Display(Name = "ItemDateTime")]
public DateTime ItemDateTime { get; set; }
[Display(Name = "ItemUserID")]
public string ItemUserID { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web;
using System.Security.Cryptography;
namespace demo.Data
{
public class DealsMasters
{
[Key]
[Display(Name = "DealID")]
public int DealID { get; set; }
[Display(Name = "DealItemID")]
public int DealItemID { get; set; }
[Required(ErrorMessage = "Please enter The name of the supplier")]
[Display(Name = "Supplier")]
public string DealSupplier { get; set; }
[Required(ErrorMessage = "Please enter the price of the supplier")]
[Display(Name = "Price")]
public Decimal DealPrice { get; set; }
[Required(ErrorMessage = "Please enter the link to the suppliers page")]
[Display(Name = "Link")]
public string DealLink { get; set; }
[Required(ErrorMessage = "Please enter if the supplier accents visa")]
[Display(Name = "Visa")]
public string DealVisa { get; set; }
[Required(ErrorMessage = "Please enter if the supplier accents clarna")]
[Display(Name = "Clara")]
public string DealClarna { get; set; }
[Display(Name = "DealUserID")]
public string DealUserID { get; set; }
}
}
@page
@model demo.Pages.GetItemModel
@{
ViewData["Title"] = "GetItem";
}
<div>
<hr />
<dl >
<dd>
<a href="#" onclick="history.go(-1)">Go Back</a>
<h1 style="text-align: left; text-transform:uppercase;"> @Html.DisplayFor(model => model.ItemsMasters.ItemName)</h1>
<hr style="height:2px;border-width:0;color:gray;background-color:gray">
</dd>
<dd>
<img src="~/ItemImages/@Model.ItemsMasters.ItemImage" alt="No Image Found"
style=" max-width:500px; max-height:500px; display: block; margin-left: auto; margin-right: auto;">
<hr style="height:1px;border-width:0;color:gray;background-color:gray">
<a asp-page="/deals/GetDeals" asp-route-id="@Model.ItemsMasters.ItemID">View deals for this item</a> |
<a asp-page="/PutItem" asp-route-id="@Model.ItemsMasters.ItemID">Edit This Item</a> |
<a asp-page="/deals/PostDeal" asp-route-id="@Model.ItemsMasters.ItemID">Create a deal for this item</a> |
<a asp-page="/DeleteItem" asp-route-id="@Model.ItemsMasters.ItemID">Delete This Item</a>
<hr style="height:1px;border-width:0;color:gray;background-color:gray">
</dd>
<dt>
Description
</dt>
<dd>
<p style="text-align: justify;">@Model.ItemsMasters.ItemDesc</p>
</dd>
<dt>
Colour
</dt>
<dd>
@Html.DisplayFor(model => model.ItemsMasters.ItemColour)
</dd>
<dt>
Catergory
</dt>
<dd>
@Html.DisplayFor(model => model.ItemsMasters.ItemCatergory)
</dd>
<dt>
Brand
</dt>
<dd>
@Html.DisplayFor(model => model.ItemsMasters.ItemBrand)
</dd>
</dl>
<table >
<thead>
<tr>
<th>
Supplier
</th>
<th>
Price
</th>
<th>
Visa
</th>
<th>
Clarna
</th>
<th>
Link
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.DealsMasters)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DealSupplier)
</td>
<td>
£@Html.DisplayFor(modelItem => item.DealPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.DealVisa)
</td>
<td>
@Html.DisplayFor(modelItem => item.DealClarna)
</td>
<td>
<p><a href="@item.DealLink" target="_blank" rel="noopener noreferrer">@item.DealLink</a></p>
</td>
<td>
<a asp-page="/deals/PutDeal" asp-route-id="@item.DealID">Edit This Deal</a> |
<a asp-page="/deals/DeleteDeal" asp-route-id="@item.DealID">Delete This Deal</a>
</td>
</tr>
}
</tbody>
</table>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using demo.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using System.Security.Claims;
namespace demo.Pages
{
public class GetItemModel : PageModel
{
private readonly demo.Data.ApplicationDbContext _context;
private int? id;
public GetItemModel(demo.Data.ApplicationDbContext context)
{
_context = context;
}
public ItemsMasters ItemsMasters { get; set; }
public DealsMasters DealsMasters { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
ItemsMasters = await _context.ItemsMasters.FirstOrDefaultAsync(m => m.ItemID == id);
this.id = id;
DealsMasters = await _context.DealsMasters.FirstOrDefaultAsync(m => m.DealItemID == this.id);
if (ItemsMasters == null)
{
return NotFound();
}
if (DealsMasters == null)
{
return NotFound();
}
return Page();
}
}
}
CodePudding user response:
im trying to have the getItem Page to show the deals table but only the entires where the DealItemId matches the ID of the item on the page
It is because the you use FirstOrDefaultAsync(m => m.DealItemID == this.id)
,try to use:
DealsMasters = await _context.DealsMasters.ToList();