1

I have a problem with my App. I have This Multiple Model:

public class MultipleModel
{
    public Staff Staff;
    public RegisterModel RegisterModel;
}

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}


public partial class Staff
{
    public Staff()
    {
        this.Tables = new HashSet<Tables>();
    }

    public int EmployeeID { get; set; }
    public string First_name { get; set; }
    public string Last_name { get; set; }
    public string Telephone { get; set; }
    public string Description { get; set; }
    public string Username { get; set; }

    public virtual ICollection<Tables> Tables { get; set; }
}

The controler is:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult MultipleModelView(MultipleModel model)
    {          
        if (ModelState.IsValid)
        {
            try
            {                   
                WebSecurity.CreateUserAndAccount(model.RegisterModel.UserName, model.RegisterModel.Password);
                Roles.AddUserToRole(model.RegisterModel.UserName, "Customer");
                Staff staff = new Staff();

                staff.Username = model.RegisterModel.UserName;
                staff.First_name = model.Staff.First_name;
                staff.Last_name = model.Staff.Last_name;
                staff.Telephone = model.Staff.Telephone;
                db.Staff.Add(staff);
                db.SaveChanges();

                WebSecurity.Login(model.RegisterModel.UserName, model.RegisterModel.Password);

                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }
        return View(model);
    }

And View is:

@model test13.Models.MultipleModel
@{
ViewBag.Title = "MultipleModelView";
}

<h2>MultipleModelView</h2>
@using (Html.BeginForm("MultipleModelView", "Account", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
    <legend>Registration Form</legend>
    <ol>
        <li>
            @Html.LabelFor(m => m.RegisterModel.UserName)
            @Html.TextBoxFor(m => m.RegisterModel.UserName)
        </li>            
        <li>
            @Html.LabelFor(m => m.RegisterModel.Password)
            @Html.PasswordFor(m => m.RegisterModel.Password)
        </li>
        <li>
            @Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
            @Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
        </li>
        <li>
            @Html.LabelFor(m => m.Staff.First_name)
            @Html.TextBoxFor(m => m.Staff.First_name)
        </li>
        <li>
            @Html.LabelFor(m => m.Staff.Last_name)
            @Html.TextBoxFor(m => m.Staff.Last_name)
        </li>
        <li>
            @Html.LabelFor(m => m.Staff.Telephone)
            @Html.TextBoxFor(m => m.Staff.Telephone)
        </li>

    </ol>
    <input type="submit" value="Register" />
</fieldset>

}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

When i Fill data to the TextBoxs and Post them, App get me Exception in this part of code:

 WebSecurity.CreateUserAndAccount(model.RegisterModel.UserName, model.RegisterModel.Password);

System.NullReferenceException: Object reference not set to an instance of an object.

Help me plese with this. Thank you.

2 Answers 2

1

Use properties and not fields in your view model:

public class MultipleModel
{
    public Staff Staff { get; set; }
    public RegisterModel RegisterModel { get; set; }
}

The default model binder in ASP.NET MVC works only with properties. Fields are ignored.

Sign up to request clarification or add additional context in comments.

1 Comment

This is IT. Thank you very much and have i nice day.
0

initializes register model in constructor

public class MultipleModel
{
    public MultipleModel()
    {
       this.RegisterModel = new RegisterModel();
    }
    public Staff Staff{get;set;};
    public RegisterModel RegisterModel{get;set;};
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.