Reputation: 335
I am new to programming, I am trying to create a shopping site like users sell their products.
I have products and a user's table. There is a "userId" column in the products table which is referenced by "ID" in the user's table. Only logged users can add product and I want to show products in each user's profile which they added. My code is below.
But here is the Error:Value cannot be null.Parameter name: source.
What is wrong with this code or is there any other way to do it.
My view:
<section class="page-section" id="relatedprod">
<div class="container">
<div class="row middle">
@foreach (Product prd in Model.Prod.Where(i=>i.userid==Model.userr.ID))
{
<div class="col-md-4">
<figure class="card card-product mehsul">
<div class="img-wrap"> <img class="img-fluid mehsulimg" src="~/PublicFront/images/30off6ec8.jpg" alt=""> </div>
<div class="handhover">
<img class="img-fluid" src="~/PublicFront/images/serv2b712.jpg" alt="">
</div>
<figcaption class="info-wrap">
<h4 class="title">@prd.ProdName</h4>
<p class="@prd.Price"></p>
</figcaption>
<div class="bottom-wrap">
<a href="" class="btn btn-sm btn-primary float-right">Order Now</a>
<div class="price-wrap h5">
<span class="price-new">$1280</span> <del class="price-old">$1980</del>
</div> <!-- price-wrap.// -->
</div> <!-- bottom-wrap.// -->
</figure>
</div> <!-- col // -->
}
</div>
</div>
</section>
Controller
public ActionResult MainAccount(int?id)
{
User us = Session["ActiveUser"] as User;
var vm = new HMViewM()
{
homesec1 = _context.homesec1slider.ToList(),
userr=us,
};
return View(vm);
}
namespace HandMShop.ViewModel
{ Models:
public class HMViewM
{
public List<homesec1slider> homesec1 { get; set; }
public User userr { get; set; }
public List<Category> catg { get;set; }
public List<Colour> colrs { get; set; }
public List<PhotoProduct> Photopr { get; set; }
public List<Product> Prod { get; set; }
}
}
public partial class Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Product()
{
this.PhotoProducts = new HashSet<PhotoProduct>();
}
public int id { get; set; }
public string ProdName { get; set; }
public string Price { get; set; }
public string Discount { get; set; }
public Nullable<int> CategoryId { get; set; }
public Nullable<int> AvailableID { get; set; }
public string Description { get; set; }
public string Material { get; set; }
public Nullable<byte> Enable { get; set; }
public Nullable<int> userid { get; set; }
public Nullable<System.DateTime> prodpostDate { get; set; }
public string ProdGenderId { get; set; }
public string sifarishle { get; set; }
public Nullable<int> LanguageId { get; set; }
public Nullable<int> colourId { get; set; }
public string Olcusu { get; set; }
public virtual AvailableTb AvailableTb { get; set; }
public virtual Category Category { get; set; }
public virtual Colour Colour { get; set; }
public virtual LanguageTb LanguageTb { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PhotoProduct> PhotoProducts { get; set; }
public virtual User User { get; set; }
}
}
Upvotes: 1
Views: 1173
Reputation: 6130
Oh okay, it seems that your product list property is empty, use the code below to fill it.
public ActionResult MainAccount(int?id)
{
User us = Session["ActiveUser"] as User;
// add Prod = _context.Product.Where(p=>p.User.ID == us.ID).ToList()
var vm = new HMViewM()
{
homesec1 = _context.homesec1slider.ToList(),
userr = us,
Prod = _context.Product.Where(p=>p.User.ID == us.ID).ToList()
};
return View(vm);
}
Upvotes: 1