C# 无法从枚举中获取字符串
我试图以字符串的形式获取某个位置的枚举结果,但由于某些原因,它在索引页中仅显示为整数 模型 枚举类 控制器 创建HTML 将Location属性设置为LocationType类型,而不是字符串 通过创建操作将位置传递给视图,如下所示C# 无法从枚举中获取字符串,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我试图以字符串的形式获取某个位置的枚举结果,但由于某些原因,它在索引页中仅显示为整数 模型 枚举类 控制器 创建HTML 将Location属性设置为LocationType类型,而不是字符串 通过创建操作将位置传递给视图,如下所示 uc = new UserLicense { Ends = ends, Starts = DateTime.Parse(col["starts"]), UserId = user.Id, LicenseId = i
uc = new UserLicense
{
Ends = ends,
Starts = DateTime.Parse(col["starts"]),
UserId = user.Id,
LicenseId = int.Parse(col["licenseid"]),
Location = uc.Location
};
然后在视图中
<select asp-for="Location" class="form-control" asp-items="@Html.GetEnumSelectList<LocationType>()">
<option selected="selected" value="">Please Select</option>
</select>
由于您正在使用IFormCollection将参数传输到控制器,无论使用公共字符串位置{get;set;}还是公共字符串位置{get;set;},在控制器中,IFormCollection中的位置值始终是整数。因此,您可以基于int值从枚举中获取字符串值:
var location = ((LocationType)int.Parse(col["location"])).ToString();
结果如下:
public class UserLicense
{
[Key]
public int License { get; set; }
public DateTime Ends { get; set; }
public DateTime Starts { get; set; }
public int UserId { get; set; }
public LocationType Location { get; set; }
}
public enum LocationType
{
Brazil,
USA,
UK
}
此外,还有另一种解决方案,您可以在action方法中使用强类型数据use UserLicense模型,而不是IFormCollection来将参数传输到控制器
代码如下:
public class UserLicense
{
[Key]
public int License { get; set; }
public DateTime Ends { get; set; }
public DateTime Starts { get; set; }
public int UserId { get; set; }
public LocationType Location { get; set; }
}
public enum LocationType
{
Brazil,
USA,
UK
}
及
Create.cshtml中的代码:
@model WebApplication1.Models.UserLicense
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Create</h1>
<h4>UserLicense</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="License" class="control-label"></label>
<input asp-for="License" class="form-control" />
<span asp-validation-for="License" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Ends" class="control-label"></label>
<input asp-for="Ends" class="form-control" />
<span asp-validation-for="Ends" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Starts" class="control-label"></label>
<input asp-for="Starts" class="form-control" />
<span asp-validation-for="Starts" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="UserId" class="control-label"></label>
<input asp-for="UserId" class="form-control" />
<span asp-validation-for="UserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Location" class="control-label"></label>
<select asp-for="Location" class="form-control" asp-items="@Html.GetEnumSelectList<LocationType>()"></select>
<span asp-validation-for="Location" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
屏幕截图如下:
您尝试过枚举的ToString方法吗?是的,我尝试过,但效果不佳。@MomenAlnaser,如果col[Location]是整数,您应该正确获得枚举的字符串名称。你试过调试代码吗?可能问题出在其他地方?添加后,我必须使用System.Text.Json.Serialization添加;作为参考,但也没有work@MomenAlnaser检查修改后的答案。您的意思是位置=uc。位置不是用户;它甚至没有打开页面,并且显示InvalidCastException:无法将类型为“System.String”的对象强制转换为类型为“System.Int32”。@Momenanaser从package manager控制台再次运行迁移。添加迁移名称,然后更新数据库。如果这不起作用,请放置一个断点以确切了解InvalidCastException的来源。或者,您可能需要使用drop database删除数据库,因为Location列中已存在具有字符串类型的行。数据库将与多种类型混淆。如果数据库已经有重要的数据,只需保留string类型的前一个location列,这样您将有两个属性,但不再需要像这样的另一个属性。publicstringlocationold和publicslocationtypelocationi在这里做了相同的操作,但是结果显示为null,而不是在您的示例中将sting显示为UK。无论如何,Location=uc.Location仍然存在一个错误,因为它总是选择第一个value@MomenAlnaser我已经发布了整个示例代码,您可以查看。在创建操作中,您使用的是UserLicense,而不是IFormCollection,对吗?尝试重新生成应用程序并清除缓存,然后检查它是否工作?@MomenAlnaser从您的原始帖子中,我注意到您正在使用Create方法创建一个新的Userlicense:`Userlicense uc=new Userlicense;`,试着在它之前设置一个断点,检查我的示例代码中的输入参数,检查userLicense,也许你是从新实例而不是从输入参数获取位置。你是对的,我从新实例获取位置,它总是获取第一个值,它得到解决,非常感谢>我已经在新实例Location=locationtypent.Parsecol[Location]中使用了下面的代码
<select asp-for="Location" class="form-control" asp-items="@Html.GetEnumSelectList<LocationType>()">
<option selected="selected" value="">Please Select</option>
</select>
var location = ((LocationType)int.Parse(col["location"])).ToString();
public class UserLicense
{
[Key]
public int License { get; set; }
public DateTime Ends { get; set; }
public DateTime Starts { get; set; }
public int UserId { get; set; }
public LocationType Location { get; set; }
}
public enum LocationType
{
Brazil,
USA,
UK
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(UserLicense userLicense)
{
if (ModelState.IsValid)
{
var location = userLicense.Location;
return RedirectToAction(nameof(Index));
}
return View();
}
@model WebApplication1.Models.UserLicense
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Create</h1>
<h4>UserLicense</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="License" class="control-label"></label>
<input asp-for="License" class="form-control" />
<span asp-validation-for="License" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Ends" class="control-label"></label>
<input asp-for="Ends" class="form-control" />
<span asp-validation-for="Ends" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Starts" class="control-label"></label>
<input asp-for="Starts" class="form-control" />
<span asp-validation-for="Starts" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="UserId" class="control-label"></label>
<input asp-for="UserId" class="form-control" />
<span asp-validation-for="UserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Location" class="control-label"></label>
<select asp-for="Location" class="form-control" asp-items="@Html.GetEnumSelectList<LocationType>()"></select>
<span asp-validation-for="Location" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>