0

I created an ASP.NET MVC4 project with authentication. When I run my app I get an error:

System.NullReferenceException

on the 17th line in file Views/Accounts/Register. How can I resolve it?

@model ImArtistApp.Models.RegisterViewModel
@{
    ViewBag.Title = "Регистрация";
}

<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Создание новой учетной записи.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            // Error here
            @Html.DisplayFor(m => m.Email, new { @class = "form-control" })    
        </div> ....

Code of model:

public class RegisterViewModel
{
        [Required]
        [EmailAddress]
        [DataType(DataType.Text)]
        [Display(Name = "Адрес электронной почты")]
        [UIHint("String")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "Значение {0} должно содержать не менее {2} символов.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Пароль")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Подтверждение пароля")]
        [Compare("Password", ErrorMessage = "Пароль и его подтверждение не совпадают.")]
        public string ConfirmPassword { get; set; }
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Filburt Jan 22 '21 at 19:23
  • No, thx, i know what is null reference exception – Vitaly Yakubin Jan 22 '21 at 19:26
  • `I created an ASP.NET MVC4` Why?! ASP.NET MVC 4 is dead. It's been replaced by ASP.NET MVC 5 and subsequently ASP.NET Core MVC. – mason Jan 22 '21 at 19:27
  • Ohhh... Ok... Maybe it was mistake to use this framework. But still i'm intersted in why this error happened – Vitaly Yakubin Jan 22 '21 at 19:30

1 Answers1

0

Do you return the Model to the controller in the Register action?

public ActionResult Register()
{
   var model = new RegisterViewModel();
   return View(model);
}
Lucas Ethen
  • 2,716
  • 1
  • 4
  • 11