Home > Back-end >  Data is not loading into Model Project
Data is not loading into Model Project

Time:01-10

Please help to find solution, data is not being loaded into model object.

Controller code:

public ActionResult ProductDetails(string ItemId)
{
    return PartialView();
}
 
[HttpPost]
public ActionResult Index(string ItemId)
{
    listOfShoppingCartModels = GetDetails(ItemId);
    return PartialView("ProductDetails", listOfShoppingCartModels);
}

ProductDetails view:

<div >
   <h2 >Product List</h2>
   @foreach (var item in Model)
   {
    <div  style="border: 2px solid black">
    <div style="text-align: center; border-bottom: 2px solid maroon">
      <h3>@item.ItemName</h3>
  </div>
  <div>
     <div >
         <img src="@Url.Content(@item.ImagePath)" width="150px" height="150px" />
     </div>
      <div  style="text-align: left">
         <b>@item.Description</b>
      </div>
</div>

Shopping view model:

public class ShoppingViewModel
{
    public Guid ItemId { get; set; }
    public string ItemName { get; set; }
    public decimal ItemPrice { get; set; }
    public string ImagePath { get; set; }
    public string Description { get; set; }
    public string ItemCode { get; set; }
    public string Category { get; set; }
}

Error:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 12:
Line 13: Product List
Line 14: @foreach (var item in Model)
Line 15: {
Line 16:

Source File: C:\Users\Sangamesh.Rawoor\Downloads\WebAppECart-master (2) (1)\WebAppECart-master\WebAppECart-master\WebAppECartDemo\Views\Shopping\ProductDetails.cshtml Line: 14

CodePudding user response:

Crate Index HTTPGET action method in order to load your data.

  • Related