Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何处理价格的小数错误?_Asp.net Core_Model View Controller_Razor - Fatal编程技术网

Asp.net core 如何处理价格的小数错误?

Asp.net core 如何处理价格的小数错误?,asp.net-core,model-view-controller,razor,Asp.net Core,Model View Controller,Razor,我正在开发这个应用程序,它应该在“localhost/catalog”上显示一些数据。我有一个用于模型和应用程序可能使用的服务的库。我得到这个错误: InvalidOperationException: The property 'Price' is not a navigation property of entity type 'StoreAsset'. The 'Include(string)' method can only be used with a '.' separated

我正在开发这个应用程序,它应该在“localhost/catalog”上显示一些数据。我有一个用于模型和应用程序可能使用的服务的库。我得到这个错误:

InvalidOperationException: The property 'Price' is not a navigation 
property of entity type 'StoreAsset'. The 'Include(string)' method can 
only be used with a '.' separated list of navigation property names.Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.WalkNavigations(IEntityType entityType, IReadOnlyList<string> navigationPropertyPaths, IncludeLoadTree includeLoadTree, bool shouldThrow)
InvalidOperationException:属性“Price”不是导航
实体类型为“StoreAsset”的属性。“Include(string)”方法可以
只能与导航属性名称的“.”分隔列表一起使用。Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.WalkNavigations(IEntityType entityType、IReadOnlyList NavigationPropertyPath、IncludeLoadTree IncludeLoadTree、bool shouldThrow)
下面是我正在使用的代码(控制器、模型和视图)和底部的服务方法:

public class CatalogController : Controller
{
    private IStoreAsset _assets;

    public CatalogController(IStoreAsset assets)
    {
        _assets = assets;
    }

    public ActionResult Index()
    {
        var assetModels = _assets.GetAll();

        var listingResult = assetModels
            .Select(result => new AssetIndexListingModel
            {
                Id = result.Id,
                Tipology = _assets.GetTipology(result.Id),
                Size = _assets.GetSize(result.Id),
                Price = decimal.Parse(_assets.GetPrice(result.Id))                 
            });

        var model = new AssetIndexModel()
        {
            Assets = listingResult
        };

        return View(model);
    }

public class AssetIndexListingModel
{
    public int Id { get; set; }
    public string Size { get; set; }
    public decimal Price { get; set; }
    public string Tipology { get; set; }
    public string ImageUrl { get; set; }
}

public abstract class StoreAsset
{        
    public int Id { get; set; }

    [Required]
    public Status Status { get; set; }

    [Required]
    public decimal Price { get; set; }

    public string ImageUrl { get; set; }
}

public class Dress : StoreAsset
{        
    [Required]
    public string Color { get; set; }

    [Required]
    public string Tipology { get; set; }

    [Required]
    public string Size { get; set; }
}

@model Models.Catalog.AssetIndexModel

<div id="assets">
<h3></h3>
<div id="assetsTable">
    <table class="table table-condensed" id="catalogIndexTable">
        <thead>
            <tr>
                <th>Size</th>
                <th>Price</th>
                <th>Tipology</th>                 
            </tr>
        </thead>
        <tbody>
            @foreach (var asset in Model.Assets)
            {
            <tr class="assetRow">
                <td class="">
                    <a asp-controller="Catalog" asp-action="Detail" asp-route-id="@asset.Id">
                        <img src="@asset.ImageUrl" class="imageCell" />
                    </a>                        
                </td>
                <td class="">@asset.Price</td>
                <td class="">@asset.Size</td>
                <td class="">@asset.Tipology</td>
            </tr>
            }
        </tbody>
    </table>
</div>
公共类CatalogController:控制器
{
私人资产;
公共CatalogController(IStoreAsset资产)
{
_资产=资产;
}
公共行动结果索引()
{
var assetModels=_assets.GetAll();
var listingResult=assetModels
.选择(结果=>new AssetIndexListingModel
{
Id=result.Id,
Tipology=_assets.GetTipology(result.Id),
Size=\u assets.GetSize(result.Id),
Price=decimal.Parse(_assets.GetPrice(result.Id))
});
var模型=新的AssetIndexModel()
{
资产=listingResult
};
返回视图(模型);
}
公共类AssetIndexListingModel
{
公共int Id{get;set;}
公共字符串大小{get;set;}
公共十进制价格{get;set;}
公共字符串Tipology{get;set;}
公共字符串ImageUrl{get;set;}
}
公共抽象类StoreAsset
{        
公共int Id{get;set;}
[必需]
公共状态状态{get;set;}
[必需]
公共十进制价格{get;set;}
公共字符串ImageUrl{get;set;}
}
公共类服装:StoreAsset
{        
[必需]
公共字符串颜色{get;set;}
[必需]
公共字符串Tipology{get;set;}
[必需]
公共字符串大小{get;set;}
}
@模型Models.Catalog.AssetIndexModel
大小
价格
歪理邪说
@foreach(Model.Assets中的var资产)
{
@资产价格
@资产规模
@资产、小费
}

公共类StoreAssetService:IStoreAsset
{
私人语境(private Context)(私人语境);;
public StoreAssetService(上下文)
{
_上下文=上下文;
}
公共作废添加(StoreAsset newAsset)
{
_添加(新资产);
_SaveChanges();
}
公共IEnumerable GetAll()
{
return\u context.StoreAssets
.Include(资产=>asset.Status)
.包括(资产=>资产价格);
}
public StoreAsset GetById(内部id)
{
//返回查询(与返回GetAll().FirstOrDefault(…)相同)
return\u context.StoreAssets
.Include(资产=>assets.Status)
.Include(资产=>assets.Price)
//因此它可以毫无问题地返回null
.FirstOrDefault(asset=>asset.Id==Id);
}
public StoreBranch GetCurrentLocation(内部id)
{
抛出新的NotImplementedException();
}
//实施和测试
公共字符串GetPrice(int-id)
{           
return _context.Dresses.FirstOrDefault(p=>p.Id==Id.Price.ToString();
}
公共字符串GetSize(int-id)
{
返回_context.Dresses.FirstOrDefault(s=>s.Id==Id).Size;
}
公共字符串GetStatus(int-id)
{
抛出新的NotImplementedException();
}
公共字符串GetTipology(int-id)
{
var dress=_context.StoreAssets.OfType()的上下文
。式中(b=>b.Id==Id);
//现在,如果不是派对礼服,请退回另一件
返回礼服。任何()?“派对”:“其他”;
}
}
我应该使用一些ForeignKey属性还是将Price更改为字符串?
任何帮助都将非常感谢

正如错误消息中指出的那样,
包含
仅用于导航属性

您需要更改以下内容:

return _context.StoreAssets
        .Include(asset => asset.Status)
        .Include(asset => asset.Price);
致:

参考:


我还有一个问题。当我转到“本地主机/目录”时该页面应显示数据库中的所有列/条目,但它只显示一列。foreach cicle中是否有错误?

此外,在该页面中,当我的数据库中有多个列时,它只返回一个数据,这是为什么?
return _context.StoreAssets
        .Include(asset => asset.Status)
        .Include(asset => asset.Price);
return _context.StoreAssets
        .Include(asset => asset.Status).ToList();