0
https://gyazo.com/0e64a4a003c33f117f114b914ccde492MVC5でユーザーロールを作成しようとしています。私のコンテキストプロパティがnullを表示しています理由はわかりません
上記の図のように、私はRegisterRoleアクションを呼び出すとNullreferenceエラーが発生しました。 ApplicationDbContextクラスでの作業は初めてです。
コントローラクラス:
private ApplicationDbContext context;
アクションメソッド:
を[HttpGet]
public ActionResult RegisterRole()
{
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
ViewBag.UserName = new SelectList(context.Users.ToList(), "UserName", "UserName");
return View();
}
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<ActionResult> RegisterRole(RegisterViewModel model, ApplicationUser user)
{
var userId = context.Users.Where(i => i.UserName == user.UserName).Select(s => s.Id);
string updatedId = "";
foreach (var i in userId)
{
updatedId = i.ToString();
}
await this.UserManager.AddToRoleAsync(updatedId, model.Name);
return RedirectToAction("Index", "Home");
}
コンテキストはどこに定義されていますか?そのアクションの前に初期化しましたか? – Teddy
はい、アカウントコントローラで割り当てました。 –
コードを表示してください。 – Teddy