数据未加载到模型项目中。

huangapple go评论50阅读模式
英文:

Data is not loading into Model Project

问题

请帮助查找解决方法,数据未加载到模型对象中。

控制器代码:

public ActionResult ProductDetails(string ItemId)
{
    return PartialView();
}

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

ProductDetails 视图:

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

购物视图模型:

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; }
}

错误:

> 服务器错误在'/ '应用程序中。
> 对象引用未设置为对象的实例。
> 描述:在执行当前Web请求时发生未经处理的异常。请查看堆栈跟踪以获取有关错误以及错误源的更多信息。
> 异常详细信息:System.NullReferenceException: 对象引用未设置为对象的实例。
> 源错误:
> 行 12:<div class="container">  
> 行 13: <h2 class="text-center">Product List</h2>  
> 行 14: @foreach (var item in Model)  
> 行 15: {  
> 行 16: <div class="col-md-4" style="border: 2px solid black">
> 
> 源文件:C:\Users\Sangamesh.Rawoor\Downloads\WebAppECart-master (2) (1)\WebAppECart-master\WebAppECart-master\WebAppECartDemo\Views\Shopping\ProductDetails.cshtml    行: 14
英文:

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(&quot;ProductDetails&quot;, listOfShoppingCartModels);
}

ProductDetails view:

&lt;div class=&quot;container&quot;&gt;
   &lt;h2 class=&quot;text-center&quot;&gt;Product List&lt;/h2&gt;
   @foreach (var item in Model)
   {
    &lt;div class=&quot;col-md-4&quot; style=&quot;border: 2px solid black&quot;&gt;
    &lt;div style=&quot;text-align: center; border-bottom: 2px solid maroon&quot;&gt;
      &lt;h3&gt;@item.ItemName&lt;/h3&gt;
  &lt;/div&gt;
  &lt;div&gt;
     &lt;div class=&quot;col-md-8&quot;&gt;
         &lt;img src=&quot;@Url.Content(@item.ImagePath)&quot; width=&quot;150px&quot; height=&quot;150px&quot; /&gt;
     &lt;/div&gt;
      &lt;div class=&quot;col-md-4&quot; style=&quot;text-align: left&quot;&gt;
         &lt;b&gt;@item.Description&lt;/b&gt;
      &lt;/div&gt;
&lt;/div&gt;

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: <div class="container">
> Line 13: <h2 class="text-center">Product List</h2>
> Line 14: @foreach (var item in Model)
> Line 15: {
> Line 16: <div class="col-md-4" style="border: 2px solid black">
>
> Source File: C:\Users\Sangamesh.Rawoor\Downloads\WebAppECart-master (2) (1)\WebAppECart-master\WebAppECart-master\WebAppECartDemo\Views\Shopping\ProductDetails.cshtml Line: 14

答案1

得分: 0

创建一个HTTPGET动作方法以加载您的数据。

英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 13:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定