1

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();
}

http://prntscr.com/kjbqt3

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.

  • 2
    You need to pass the model to your view. `return View(registered);` (instead of `return View();`) – Igor Aug 16 '18 at 18:29
  • Looks like you are not passing a viewmodel back to your view like in the method above! – Yves Rochon Aug 16 '18 at 18:30
  • I did that! its still giving me the null reference. – Abdul Muqeet Khan Aug 16 '18 at 18:38
  • Based on your screenshot - you `do` get `UserRoleId` of `2`. Were you also expecting getting a list of all the Roles? – mike123 Aug 16 '18 at 18:46
  • @Igor its not the issue with the return View (registered) either. i have written that and done that if you want the proof i can show you that as well. – Abdul Muqeet Khan Aug 16 '18 at 18:47
  • @mike123 no i knew that i was supposed to get just 2 what i dont get it is that after that when i press the Debugger i just get a null exception. if i remove the html drop down list the code functions perfectly and i am able to check for the validations that i have done through DataAnnotations – Abdul Muqeet Khan Aug 16 '18 at 18:50
  • @AbdulMuqeetKhan - ah got it. Sometimes visual studio re-uses cached assembly. Try may be clear the temporary cache files - described here https://stackoverflow.com/questions/16137457/asp-net-temporary-files-cleanup and see if that solves the issue – mike123 Aug 16 '18 at 18:53
  • @mike123 thanks a lot il try and look at that. and come back to you. – Abdul Muqeet Khan Aug 16 '18 at 18:57
  • @mike123 emm its still not working. i am still getting a null exception. – Abdul Muqeet Khan Aug 16 '18 at 19:12
  • You have not repopulated the value of `UserRoles` in the POST method (and then it needs to be `return View(registered)` (but why are you returning the view anyway (you only do that if `ModelState` is invalid, otherwise you redirect) –  Aug 16 '18 at 22:30
  • And your view is based on `RegisterViewModel`, therefore the parameter in the POST method also needs to be `RegisterViewModel`, not `User` –  Aug 16 '18 at 22:31
  • @StephenMuecke thanks alot man your comment solved my Issue it was the issue of not repopulating and it worked well. as for returning the View its simply because i also wanted to test the validation as well.Now i will work on redirecting. Once again thanks a lot. – Abdul Muqeet Khan Aug 17 '18 at 12:42

0 Answers0