okay pretty simple i am simply trying to create a register form where i want to register a person as either a candidate or company or admin based on the choice once i register them il redirect them.
the thing is that my form works great until i try and implement the Drop down list.
here is the code that i am implemented in View
<div class="page-login-form">
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new {@class = "login-form"}))
{
@Html.LabelFor(m=>m.User.Name)
@Html.TextBoxFor(m=>m.User.Name, new { @class = "form-control" })
@Html.LabelFor(m=>m.User.Email )
@Html.TextBoxFor(m=>m.User.Email, new { @class = "form-control" })
@Html.LabelFor(m=>m.User.Password )
@Html.PasswordFor(m=>m.User.Password, new { @class = "form-control" })
<div class="alert-danger"> @Html.ValidationMessageFor(m => m.User.Password)</div>
@Html.LabelFor(m => m.User.ConfirmPassword)
@Html.PasswordFor(m => m.User.ConfirmPassword, new { @class = "form-control" })
<div class="alert-danger"> @Html.ValidationMessageFor(m => m.User.ConfirmPassword)</div>
@Html.DropDownListFor(m=>m.User.UserRoleId,new SelectList(Model.UserRoles,"Id","Name"),"select your registration", new { @class = "form-control" })
<button type="submit" class=“btn btn-‐primary”>Save</button>
}
</div>
Here is the ViewModel that i implemented
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Visume.Models;
namespace Visume.ViewModel
{
public class RegisterViewModel1
{
public IEnumerable<UserRole> UserRoles { get; set; }
public User User { get; set; }
}
}
This is the Code in the Controller
public ActionResult Register()
{
var userrole = _context.UserRoles.ToList();
var viewmodel=new RegisterViewModel1
{
UserRoles=userrole
};
return View(viewmodel);
}
--------------------------------------------------------------
[HttpPost]
public ActionResult Register( User registered)
{
return View();
}
Now the error is extremely simple and can be seen in the print screen
when i run my Debugger this is what i get https://prnt.sc/kjcr6r basically what i can see is that the data is being passed from View to the Controller. Look at the UserRoleID, its clearly 2 yet when i proceed further i still get the error that Null reference is not handled. Where am i going wrong and why. This is a learning project and i can fully share the files if you guys want to test things yourself as well. Please be helpfull.