ArgumentNullException:值不能为null.参数名称:viewData

编程入门 行业动态 更新时间:2024-10-27 18:24:03
本文介绍了ArgumentNullException:值不能为null.参数名称:viewData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的登录页面,如下所示:

I have simple login page like this:

Login.cshtml

Login.cshtml

@page @model GDPR.Views.Account.LoginModel @{ } @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers <html> <body> <p>Login </p> <form method="post"> <div asp-validation-summary="All"></div> Username: <input asp-for="loginData.Username" value="username" /><br /> Password: <input asp-for="loginData.Password" value="password" /><br /> <br /> <input type="submit" value="Login" /> @Html.AntiForgeryToken() </form> </body> </html>

和后面的代码:

public class LoginModel : PageModel { [BindProperty] // Bind on Post public LoginData loginData { get; set; } public async Task<IActionResult> OnPostAsync() { if (ModelState.IsValid) { ....... await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); return RedirectToPage("/Home/Index"); } else { ModelState.AddModelError("", "username or password is blank"); return Page(); } } public class LoginData { [Required] public string Username { get; set; } [Required, DataType(DataType.Password)] public string Password { get; set; } } }

运行该应用程序时,出现错误:ArgumentNullException值不能为null,参数名称:viewData.

When I run the app I get an error: ArgumentNullException value cannot be null, param name : viewData.

该错误的原因可能是什么?

What could be the reason for that error?

推荐答案

您在第一行上的@page为null,这是导致错误的原因.删除@page语句或使用它来定义要显示的页面.

Your @page on the first line is null which is what's causing the error. Either remove the @page statement or use it to define the page to be displayed.

更多推荐

ArgumentNullException:值不能为null.参数名称:viewData

本文发布于:2023-10-22 22:13:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能为   名称   参数   ArgumentNullException   viewData

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!