简体   繁体   中英

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

I'm using 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

but I countered a problem. I did pretty much the same.

User Model

public class ApplicationUser : IdentityUser

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

game model

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

Register.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

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

GameID = Input.GameID

In razor remove that select line from that link and change it to

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM