英文:
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("ProductDetails", listOfShoppingCartModels);
}
ProductDetails
view:
<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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论