简体   繁体   English

CS0029 C# 无法将类型隐式转换为“int?” ASP.NET CORE 5 MVC 标识

[英]CS0029 C# Cannot implicitly convert type to 'int?' ASP.NET CORE 5 MVC Identity

I'm using ASP.NET Core 5 MVC Identity.我正在使用 ASP.NET Core 5 MVC Identity。 I want to customise the registration and adding a selection list from another table, that user can decide to choose an option or not.我想自定义注册并从另一个表中添加一个选择列表,该用户可以决定是否选择一个选项。 So I'm following the answer from this ASP.NET Core Identity Model Customization Select List所以我正在关注这个ASP.NET Core Identity Model Customization Select List的答案

but I countered a problem.但我反驳了一个问题。 I did pretty much the same.我做的差不多。

User Model用户 Model

public class ApplicationUser : IdentityUser

        [ForeignKey("GameID")]
        public int? GameID { get; set; }

game model游戏 model

        public int ID { get; set; }
        public string GameName { get; set; }
        public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }

Register.cshtml.cs注册.cshtml.cs

private readonly ApplicationDBContext _context
public IEnumerable<SelectListItem> GameList { get; set; }

RegisterModel{}()part
ApplicationDBContext context
_context = context;

InputModel
public int? GameID { get; set; }

OnGetAsync
GameList = availableLevels.Select(x => new SelectListItem() { Text = x.GameName, Value = x.ID.ToString() }).ToList();

OnPostAsync
GameID = _context.Games.Where(x => x.ID == Input.GameID)

I don't have experience posting stuff and I'm a noob at coding.我没有发布东西的经验,而且我是编码的菜鸟。 Hope it makes sense希望有意义

Try something like this in your register.cshtml在你的 register.cshtml 中尝试这样的事情

in OnGetAsync

GameList = _context.model name in your DBcontext.Select(x => new SelectListItem { Text = x.yourgames???.ToString(), Value = x.ID.ToString() }).ToList();

and on your OnPostAsync keep it simple并在您的 OnPostAsync 上保持简单

GameID = Input.GameID

In razor remove that select line from that link and change it to在 razor 中,从该链接中删除 select 行并将其更改为

<select asp-for="Input.GameID" asp-items="Model.GameList" class="form-control">

I don't know how the person on the link does it, but in the context of that link and the post is about the selection in register form I beleive.我不知道链接上的人是如何做到的,但是在该链接的上下文中,帖子是关于我相信的注册表中的选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 收到错误消息:CS0029:无法隐式转换(ASP.NET Core MVC 和 C#) - Getting error message: CS0029: Cannot implicitly convert (ASP.NET Core MVC & C#) C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate” - C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate' CS0029 C#无法将类型隐式转换为&#39;string []&#39; - CS0029 C# Cannot implicitly convert type to 'string[]' 我得到了 CS0029 C# 无法将类型“int”隐式转换为“bool” - i got CS0029 C# Cannot implicitly convert type 'int' to 'bool' 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score' CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39; - CS0029: Cannot implicitly convert type 'int' to 'bool' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' C#错误:无法将类型&#39;string&#39;隐式转换为&#39;System.Windows.Forms.Control&#39;(CS0029) - c# Error: Cannot implicitly convert type 'string' to 'System.Windows.Forms.Control' (CS0029)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM