gpt4 book ai didi

c# - 将枚举用于 DropdownList : Missing a using directive or assembly reference

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:00 26 4
gpt4 key购买 nike

我正在尝试在 ASP.NET Core 中创建一个带有枚举属性的下拉列表。

这是模型(viewmodel):

namespace Application.Models.ApplicationviewModels
{
public class StoreIndexData
{
[Display(Name = "Departamento")]
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }

[Display(Name = "Distrito")]
public int DistrictID { get; set; }
public string DistrictName { get; set; }
[EnumDataType(typeof(tiendacadenaenum))]
public tiendacadenaenum tienda_cadena {get;set;}
}
public enum tiendacadenaenum
{
[Display(Name = "Cencosud")]
Cencosud,
[Display(Name = "Cinerama")]
Cinerama,
}
}

这是 View 的一部分:

@model Application.Models.ApplicationviewModels.StoreIndexData
@using Application.Models

<div class="form-group">
<label asp-for="tienda_cadena" class="cold-md-2"></label>
<div class="col-md-10">
<select asp-for="tienda_cadena" class="form-control"
asp-items="Html.GetEnumSelectList<tiendacadenaenum>()">"></select>
<span asp-validation-for="tienda_cadena" class="text-danger"></span>
</div>
</div>

这里显示有问题:

Html.GetEnumSelectList<tiendacadenaenum>()">

声称缺少 using 指令或程序集引用。关于如何解决这个问题的任何想法?

enter image description here

最佳答案

添加 @using Application.Models.ApplicationviewModels 到 View 。

还建议使用命名约定,因为这可以让其他人更容易阅读您的代码,特别是对枚举名称、属性名称、命名空间名称和方法名称使用 PascalCase。

所以代码应该是这样的:

namespace Application.Models.ApplicationViewModels
{
public class StoreIndexData
{
[Display(Name = "Departamento")]
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }

[Display(Name = "Distrito")]
public int DistrictId { get; set; }
public string DistrictName { get; set; }
[EnumDataType(typeof(TiendaCadenaEnum))]
public TiendaCadenaEnum TiendaCadena { get; set; }
}

public enum TiendaCadenaEnum
{
[Display(Name = "Cencosud")]
Cencosud,
[Display(Name = "Cinerama")]
Cinerama,
}
}

@model Application.Models.ApplicationviewModels.StoreIndexData
@using Application.Models.ApplicationviewModels
@using Application.Models

<div class="form-group">
<label asp-for="TiendaCadena" class="cold-md-2"></label>
<div class="col-md-10">
<select asp-for="TiendaCadena" class="form-control"
asp-items="Html.GetEnumSelectList<TiendaCadenaEnum>()">"></select>
<span asp-validation-for="TiendaCadena" class="text-danger"></span>
</div>
</div>

关于c# - 将枚举用于 DropdownList : Missing a using directive or assembly reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52176523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com