-1

Error:

The model item passed into the dictionary is of type 'EventManagmentSystem.Models.RegisterPageModelViewModel', but this dictionary requires a model item of type 'EventManagmentSystem.Models.RegisterViewModel'.

Controller:

namespace EventManagmentSystem.Controllers
{
    public class CustomerController : Controller
    {
        // GET: Customer
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Register(RegisterPageModelViewModel rm)
        {
            if(ModelState.IsValid)
            {

                RegistrationPageModel rp = new RegistrationPageModel
                {
                    C_Name = rm.C_Name,
                    C_Password = rm.C_Password
                };
            }

            return View(rm);
        }
    }
}

Views

RegisterPageModelViewModel.cshtml:

@model EventManagmentSystem.Models.RegisterPageModelViewModel

@using (Html.BeginForm("Register", "Customer",FormMethod.Post, new { @class = "form-inline", role = "form" }))
{
    @Html.AntiForgeryToken()

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

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

Register.cshtml:

@model EventManagmentSystem.Models.RegisterViewModel

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>RegisterViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", 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.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
            </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>
}

Models

RegisterPageModelViewModel.cs:

namespace EventManagmentSystem.Models
{
    public class RegisterPageModelViewModel
    {
        [Required(ErrorMessage = "Name is Required")]
        public string C_Name { get; set; }

        [Required(ErrorMessage = "Password is Required")]
        public string C_Password { get; set; }
    }
}

RegistrationPageModel.cs:

namespace EventManagmentSystem.Models
{
    public class RegistrationPageModel
    {
        public int C_ID { get; set; }
        public string C_Name { get; set; }
        public string C_Password { get; set; }
        public string C_Gender { get; set; }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Check what View should be returned by Register in controller. Now it returns Register.cshtml view and model with type RegisterPageModelViewModel. But Register.cshtml requires model with type RegisterViewModel and you got this error.

Depending on what you need to do change the type of the model with RegisterViewModel. Could be like:

public ActionResult Register(RegisterPageModelViewModel rm)
{        
    ... 
    RegisterViewModel model = new RegisterViewModel{...};
    return View(model);
}

Or change the View to be as following:

public ActionResult Register(RegisterPageModelViewModel rm)
{        
    ... 
    return View("RegisterPageModelViewModel", rm);
}