Send info to the view from another table with navigation property? - asp.net

This is my models
public class Product
{
[Key]
public int SerialNumber { get; set; }
public int PartNumber { get; set; }
public string Description { get; set; }
public virtual ICollection<Reading> Reading { get; set; }
}
public class Reading
{
[Key]
public int Id { get; set; }
public int SerialNumber { get; set; }
public int ReadingValue { get; set; }
public virtual Product Product { get; set; }
}
I can send all products to the view with
return View(db.Products.ToList().Where(product => product.CustomerID == Customer));
And I can get the latest ReadingValue if I know the Product SerialNumber
var LatestReading = db.Readings.OrderByDescending(m => m.Id).Where(s => s.SerialNumber == SerialNumber).Select(m => m.ReadingValue).FirstOrDefault();
How can I send all the products to the view with the latest ReadingValue for each product?

Create a new view model that will hold both the data:
public class FooViewModel
{
public List<Product> Products { get; set; }
public Reading LatestReading { get; set; }
}
Change your view to use the new model with:
#model FooViewModel
Then send them back in your controller:
var model = new FooViewModel();
model.Products = db.Products.ToList().Where(product => product.CustomerID == Customer);
model.LatestReading = db.Readings.OrderByDescending(m => m.Id).Where(s => s.SerialNumber == SerialNumber).Select(m => m.ReadingValue).FirstOrDefault();
return View(model);

Because you have Reading property in Products class, you can get the latest ReadingValue in the view:
foreach(Product product in Model)
{
var latestReadingValue = product.Reading.OrderByDescendin(m => m.Id).FirstOrDefault();
// do what you want here
}
but as hutchonoid points out the better option is creating a ViewModel for it, because having logic in the view is a bad practice, and it doesn't correspond to MVC pattern.

Related

Many to Many crud operations in asp.net core

I have two entities (Product and Supply) that have a many-to-many relationship. I also have an entity between then that holds the two ID's (SupplyProduct).
My entities:
public class Product
{
[Key]
public int ProductId { get; set; }
[Required]
public string? ProductName { get; set; }
[Required]
[Column(TypeName = "decimal(6,2)")]
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public string? Brand { get; set; }
public DateTime CreatedDateTime { get; set; } = DateTime.Now;
//Many to many relationship between the products and the stocks
public virtual ICollection<SupplyProduct>? SupplyProducts { get; set; }
}
public class Supply
{
[Key]
[Required]
public int SupplyId { get; set; }
[Required]
[DisplayName("Supply's Label")]
public string? Label { get; set; }
//One to many relationship between the Stock and the Merchant
public Merchant? Merchant { get; set; }
//Many to many relationship between the stocks and the products
public virtual ICollection<SupplyProduct>? SupplyProducts { get; set; }
}
public class SupplyProduct
{
[Key]
public int SupplyId { get; set; }
public virtual Supply? Supply { get; set; }
[Key]
public int ProductId { get; set; }
public virtual Product? Product { get; set; }
}
I want to assign a supply to a product while creating it . and then show the supply with it's associated products
this is my products controller:
ProductsController.cs
public class ProductController : Controller
{
private readonly ApplicationDbContext _db;
public ProductController(ApplicationDbContext db)
{
_db = db;
}
// GET: ProductController
public ActionResult Index()
{
IEnumerable<Product> ProductsList = _db.Products;
return View(ProductsList);
}
// GET: ProductController/Create
public ActionResult Create()
{
IEnumerable<Supply> SuppliesList = _db.Supplies.Include(s => s.Merchant);
ViewBag.Supplies = SuppliesList;
return View();
}
// POST: ProductController/Create
[HttpPost]
public ActionResult Create(Product model, List<int> supplyIds)
{
_db.Products.Add(model);
_db.SaveChanges();
SupplyProduct SP = new();
foreach (var supplyId in supplyIds)
{
SP.SupplyId = supplyId;
SP.ProductId = model.ProductId;
SP.Product = model;
SP.Supply = _db.Supplies.Where(x => x.SupplyId == supplyId).FirstOrDefault();
}
_db.SupplyProducts.Add(SP);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
}
Can you please check my post Create method if it is as it should be, and how can I get the Products data while returning the Supplies in the Index method into the index view?
Thank you so much for your help and happy coding :D
Can you please check my post Create method if it is as it should be
Modify your code like below, otherwise you will always store the second supply in supplyIds:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Product model, List<int> supplyIds)
{
_context.Product.Add(model);
_context.SaveChanges();
SupplyProduct SP = new();
foreach (var supplyId in supplyIds)
{
SP.SupplyId = supplyId;
SP.ProductId = model.ProductId;
SP.Product = model;
SP.Supply = _context.Supply.Where(x => x.SupplyId == supplyId).FirstOrDefault();
_context.SupplyProducts.Add(SP); //move to here...
_context.SaveChanges();
}
// _context.SupplyProducts.Add(SP);
//_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
how can I get the Products data while returning the Supplies in the Index method into the index view?
Change your Index method like below:
// GET: Products
public async Task<IActionResult> Index()
{
var data = await _context.Product.Include(p => p.SupplyProducts)
.ThenInclude(sp => sp.Supply).ToListAsync();
return View(data);
}
You can remove the SupplyProduct tabble if there are no additional properties in anything other than Supply Product you don't need it for many-to many.
Then initialize the collections in the Supply and Product
public class Product
{
public Product()
{
this.Supplys = new HashSet<Supply>();
}
//... your props
public virtual ICollection<Supply> Supplys { get; set; }
}
public class Supply
{
public Supply()
{
this.Products = new HashSet<Product>();
}
//... your props
public virtual ICollection<Product> Products { get; set; }
}
Add Product to Supplys with only one query (in your code you make query for everyone Id in supplyIds)
[HttpPost]
public ActionResult Create(Product model, List<int> supplyIds)
{
//Get all supplys you need by id
var supplys = _db.Supplys
.Where(x => supplyIds.Contains(x.SupplyId))
.ToList();
//Add product in each supply
foreach (var supply in supplys)
{
supply.Products.Add(model);
}
//Update db
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
Get from DB
public ActionResult GetSuplys(List<int> supplyIds)
{
//Here you get all Supplys with the Products in it
var supplys = _db.Supplys
.Include(x => x.Products)
.Where(x => supplyIds.Contains(x.SupplyId))
.ToList();
//...
}
Save new Supply of Product
public ActionResult NewSuply()
{
var supply = new Supply
{
ProductName = name,
//Add all props you need
//You can add Product here or add empty collection
Products.Add(product), or = new List<Product>();
}
//No need to save Product separate
_db.Add(supply);
_db.SaveChanges();
}

EF Core 3.18 get sum and count from related table

I have a web api where I am trying to get sum and count of a related table. Using .net core 3 and EF Core 3.1.8.
This is what I have tried:
_context.Books
.Include(r => r.BookCategories)
.Include(r => r.Resources)
.Include(r => r.Ratings.GroupBy(g => g.Bookid).Select(s => new { SumAllVotes = s.Sum(item => item.Rating) }))
.ToListAsync();
But I just get an error message. (see below).
I find it difficault debugging with EF Core as I dont know where it is going wrong. Have been trying a couple of hours, but whatever I write I get the same error message.
Thought maybe you guys were able to see what was wrong.
What I want
I am trying to get Sum of all Rating inside table Ratings.
Rating contains only 0 or 1. And I am trying to sum ratings on each bookid. I wanted to have it in this class public int SumAllVotes { get; set; }.
Because I list out all Books...and one of the properties will then be SumAllVotes. (And also CountAllVotes, when I have finished this problem).
By the end I will have a SumAllVotes and CountAllVotes and can calculate the percentage of how many have pressed "1".
Error message:
An unhandled exception occurred while processing the request.
InvalidOperationException: Lambda expression used inside Include is
not valid.
Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessInclude(NavigationExpansionExpression
source, Expression expression, bool thenInclude)
What I have tried:
[HttpGet]
public async Task<ActionResult<IEnumerable<Books>>> GetBooks()
{
Guid userid = Guid.Parse(this.User.FindFirst(ClaimTypes.NameIdentifier).Value);
return await _context.Books
.Include(r => r.BookCategories)
.Include(r => r.Resources)
.Include(r => r.Ratings.GroupBy(g => g.Bookid).Select(s => new { SumAllVotes = s.Sum(item => item.Rating) }))
.ToListAsync();
}
Books and Ratings are defined as -
public partial class Books
{
public Books()
{
Bookmarks = new HashSet<Bookmarks>();
Comments = new HashSet<Comments>();
Favourites = new HashSet<Favourites>();
BookCategories = new HashSet<BookCategories>();
Resources = new HashSet<Resources>();
Ratings = new HashSet<Ratings>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public byte Scaleoffun { get; set; }
public byte Scaleoflearning { get; set; }
public int? Goal { get; set; }
public int? Secondgoal { get; set; }
public int? Thirdgoal { get; set; }
public int? Subjectid { get; set; }
public int? Categoryid { get; set; }
public string Language { get; set; }
public string Estimatedtime { get; set; }
public string Image { get; set; }
public int? File { get; set; }
public int? Ownerid { get; set; }
public DateTime Createdon { get; set; }
public DateTime? Lastmodifiedon { get; set; }
public string Active { get; set; }
public string Url { get; set; }
public Guid Userid { get; set; }
public byte? Grade { get; set; }
[NotMapped]
public int SumAllVotes { get; set; }
[NotMapped]
public int CountAllVotes { get; set; }
public virtual Categories Category { get; set; }
public virtual Curriculum GoalNavigation { get; set; }
public virtual Users Owner { get; set; }
public virtual Curriculum SecondgoalNavigation { get; set; }
public virtual Subjects Subject { get; set; }
public virtual Curriculum ThirdgoalNavigation { get; set; }
public virtual ICollection<Bookmarks> Bookmarks { get; set; }
public virtual ICollection<Comments> Comments { get; set; }
public virtual ICollection<Favourites> Favourites { get; set; }
public virtual ICollection<BookCategories> BookCategories { get; set; }
public virtual ICollection<Resources> Resources { get; set; }
public virtual ICollection<Ratings> Ratings { get; set; }
}
public partial class Ratings
{
public int Id { get; set; }
public int? Bookid { get; set; }
public string Type { get; set; }
public Int16? Rating { get; set; }
public Guid Userid { get; set; }
public string Subject { get; set; }
public DateTime Createdon { get; set; }
public DateTime? Modifiedon { get; set; }
public byte? Active { get; set; }
public virtual Books Book { get; set; }
//public virtual Users User { get; set; }
}
These are some other solutions I have tried, but got the same error message:
.Include(r=> r.Ratings.Sum(i=>i.Rating))
and
.Include(r => new { m = r.Ratings.GroupBy(g => g.Bookid) })
You don't need to group child entities by parent's Id. When you Include one-to-many child entities, they are added to their parent's child list, and hence grouped by their parent's identity, based on the relationship between them. All you need to do is tell EF what values you want from that child list.
Change your query to -
_context.Books
.Include(r => r.BookCategories)
.Include(r => r.Resources)
.Include(r => r.Ratings)
.Select(p => new
{
// set ALL the primitive properties from Books entity
Id = p.Id,
Title = p.Title,
// etc ...
// set the computed properties
CountAllVotes = p.Ratings.Count,
SumAllVotes = p.Ratings.Sum(x => x.Rating)
// set the related entities
BookCategories = p.BookCategories,
Resources = p.Resources
})
.ToListAsync();
AutoMapper has a ProjectTo method that generates the required query and does the projection (the Select part) automatically. You can use that to avoid the hassle of setting all those properties manually.
I suggest you don't use Include with Select. Read article how to make queries with Projection (Select). Note, that Rating.Rating is nullable and you need to handle this. Here is a possible code sample:
var view = await _context.Books
.Where(your condition)
.Select(item => new
{
//Todo: fill other props
SumAllVotes = item.Ratings.Sum(rating => (Int16?) rating.Rating),
CountAllVotes = item.Ratings.Count,
})
.ToListAsync()

How can I initialize one model in another model's controller action?

I created a web-app in Asp.net MVC and it has an order action. I have these two models for Order
public class Order
{
public int Id { get; set; }
public DateTimeOffset OrderTime { get; set; }
[InverseProperty("Order")]
public ICollection<OrderDetail> OrderDetails { get; set; }
}
and for OrderDetail
public class OrderDetail
{
public int Id { get; set; }
public int OrderId { get; set; }
public ICollection<Order> Order { get; set; }
public int MenuId { get; set; }
public int RestaurantId { get; set; }
public Menu Menu { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
And I created tables for them.
Also I created a controller for Order. It contains Index and Details actions. Index acction shows the list of order and every order has its own Detail link which should contain information of Order and related OrderDetail
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Order order = db.Orders.Find(id);
if (order == null)
{
return HttpNotFound();
}
return View(order);
}
And the problem is that OrderDetails is null. Can you suggest me how I can initialize OrderDetail in Details action?
You have to tell EntityFramework which navigation properties you want to include.
Order order = db.Orders
.Where( o => o.Id == id )
.Include( o => o.OrderDetails )
.SingleOrDefault();
But you cannot use Find method any more

Can't access number of items on navigational property

I'm not sure if this issue is an Automapper-issue or a Entity Framework-issue.
I'm having trouble getting the number of products from the navigation property ProductCount in my viewmodel. The value returned is always "0". If the function worked, it would return "no" on a category with no products and "15" on a category with 15 products.
The viewmodel:
public class ViewModelProductCategory
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public ViewModelProductCategory ParentCategory { get; set; }
public IEnumerable<ViewModelProductCategory> Children { get; set; }
public IEnumerable<ViewModelProduct> Products { get; set; }
public string ProductCount
{
get
{
return Products != null
? Products.Count().ToString()
: "no";
}
}
}
To display number of products:
#model List<MyStore.Models.ViewModels.ViewModelProductCategory>
#foreach (var item in Model)
{
#item.Title has #item.ProductCount product(s).
}
I have also tried to use #item.Products.Count() in the view, but that always returns 0.
This is how the viewmodel gets populated:
// Getting the categories
List<ProductCategory> DbCategories = _context.ProductCategories
.Include(e => e.Children).ToList().OrderBy(o => o.SortOrder)
.Where(e => e.ParentId == null).ToList();
// Mapping to ViewModel
List<ViewModelProductCategory> MapCategory =
_mapper.Map<List<ProductCategory>, List<ViewModelProductCategory>>(DbCategories);
The mapping:
CreateMap<ProductCategory, ViewModelProductCategory>()
.ForMember(dst => dst.Products, opt => opt.MapFrom(
src => src.ProductInCategory.Select(pc => pc.Product)));
ProductInCategory is a linking table between categories and products:
public class ProductInCategory
// A linking table for which products belongs to which categories
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductCategoryId { get; set; }
public int SortOrder { get; set; }
// Nav.props.:
public Product Product { get; set; }
public ProductCategory ProductCategory { get; set; }
}
Why can't I get the number of products?
Edit
With the help of #IvanStoev in the comments, I changed the query to this:
//get all categories, so we have each and every child in Context
List<ProductCategory> DbCategories = _context.ProductCategories
.Include(e => e.Children)
.Include(e => e.ProductInCategory)
.ThenInclude(p => p.Product)
.ToList().OrderBy(o => o.SortOrder)
.Where(e => e.ParentId == null).ToList();
Now it works! Yay!

Why won't Html.ListBoxFor() highlight current selected items?

I am trying to understand why my Html.ListBoxFor() is not highlighting current selected items when the view loads.
I have a database model:
public class Issue
{
[Key]
public int IssueId { get; set; }
public int Number { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public virtual ICollection<Creator> Creators { get; set; }
}
public class Creator
{
[Key]
public int CreatorId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Issue> Issues { get; set; }
}
public class Icbd : DbContext
{
public DbSet<Issue> Issues { get; set; }
public DbSet<Creator> Creators { get; set; }
}
I then have an editing model:
public class IssueEditModel
{
public Issue Issue { get; set; }
public IEnumerable<Creator> Creators { get; set; }
public IEnumerable<Creator> SelectedCreators { get {return Issue.Creators;} }
}
Then, in my controller I populate IssueEditModel:
public ActionResult EditIssue( int id = 0 )
{
IssueEditModel issueEdit = new IssueEditModel{
Creators = db.Creators.ToList(),
Issue = new Issue{ Creators = new List<Creator>()},
};
if (id > 0)
{
issueEdit.Issue = db.Issues.Include("Creators").Where(x => x.IssueId == id).Single();
}
return View(issueEdit);
}
This populates all objects correctly (as far as I can tell, anyway.) In my View, I am writing a listbox like this:
<%: Html.ListBoxFor(
x => x.SelectedCreators,
new SelectList(
Model.Creators,
"CreatorId",
"LastName"
)
)%>
This lists all the options correctly, but I cannot get the currently select items to highlight. I almost want to write my own Html Helper because this is such a simple operation, I don't understand why this is being so difficult.
Why wont the Html Helper highlight the current items?
You need a list of scalar types as first argument to the ListBoxFor helper which will map to the creator ids that you want preselected:
public class IssueEditModel
{
public IEnumerable<Creator> Creators { get; set; }
public IEnumerable<int> SelectedCreatorIds { get; set; }
}
and then:
IssueEditModel issueEdit = new IssueEditModel
{
Creators = db.Creators,
SelectedCreatorIds = db.Creators.Select(x => x.CreatorId)
};
and in the view:
<%: Html.ListBoxFor(
x => x.SelectedCreatorIds,
new SelectList(
Model.Creators,
"CreatorId",
"LastName"
)
) %>
The example submitted by Darin is ALMOST correct, but there is a slight error. Presently, his example will preselect ALL creators! However, the desired result is to only preselect the creators associated with a particular instance of Issue.
So this:
IssueEditModel issueEdit = new IssueEditModel
{
Creators = db.Creators,
SelectedCreatorIds = db.Creators.Select(x => x.CreatorId)
};
Should be this:
IssueEditModel issueEdit = new IssueEditModel
{
Creators = db.Creators,
SelectedCreatorIds = CurrentIssue.Creators.Select(x => x.CreatorId)
};
Where CurrentIssue is an instantiation of the Issue class (presumably previously populated from the datastore).

Resources