0

i created a login page and a view model for my login page

when i run created page this line will thrown an exception :

@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })

this is complete login pages code:

@model DataLayer.LoginViewModel


<div class="form-horizontal">
    <h4>LoginViewModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Rememberme, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Rememberme)
                @Html.ValidationMessageFor(model => model.Rememberme, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

}

and this is my viewmodel :

 public class LoginViewModel
{
    [Required]
    [MaxLength(150)]
    public string UserName { get; set; }

    [Required]
    [MaxLength(150)]
    public string Password { get; set; }
   
    public bool Rememberme { get; set; }
}

this is controller :

ILoginRepository loginRepository;
    MyCmsContext db = new MyCmsContext();
    public AccountController()
    {
        loginRepository = new LoginRepository(db);
    }
    // GET: Account
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(LoginViewModel login)
    {
        if (ModelState.IsValid)
        {
            if (loginRepository.IsExistUser(login.UserName,login.Password))
            {
                FormsAuthentication.SetAuthCookie(login.UserName, login.Rememberme);
                return Redirect("/");
            }
            else
            {
                ModelState.AddModelError("UserName", "User not found");
            }
        }
        return View();
    }
}
Panagiotis Kanavos
  • 90,087
  • 9
  • 138
  • 171
Beginner
  • 7
  • 2
  • Please share the Controller code as well – Yogi Mar 26 '21 at 06:23
  • I edited my question and shared my controller – Beginner Mar 26 '21 at 06:50
  • This is a very common error, with an exact duplicate question. You didn't post the actual exception, but the controller *doesn't* provide a model to the view. This means that `model`, assuming that's how the View's model is named, is `null` – Panagiotis Kanavos Mar 26 '21 at 06:50
  • Why are you writing your own login page in the first place, when ASP.NET MVC *already* has login pages? If you want to connect to a different database just change the connection string. If you want to modify the User object, just do that. There's nothing special about the User or the DbContext it uses, they're just a DTO and a DbContext. If you wanted to write your own login page just to use an `ILoginRepository` interface - why? What problem is solved by *writing* extra untested code to replace what's already available? – Panagiotis Kanavos Mar 26 '21 at 06:53
  • In any case, if you want to write your own login page, you can modify the existing one. – Panagiotis Kanavos Mar 26 '21 at 06:54
  • I can write my login page by "identity2" I think write my own login page is hard – Beginner Mar 26 '21 at 06:57
  • You are not passing `model` in your view. – Yogi Mar 26 '21 at 07:02

1 Answers1

0

Keep the code simple

  1. Make sure you are instantiating the viewmodel inside the Action Method and passing it to the view as shown below

    [HttpGet]
    public ActionResult Login()
    {
     LoginViewModel lgVM= new LoginViewModel();
     return View(lgVM);
    }
    [HttpPost]
    public ActionResult Login(LoginViewModel login)
    {
    
     return View();
    }
    
  2. Keep your markup simple as

    @Html.EditorFor(model => model.UserName, new{ @class = "form-control" })
    
Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
Sachin Singh
  • 124
  • 9