0

I have been searching around for almost 3 days to find a solution to the following problem. I'm simply trying to add a DropDownList in RegisterViewModel (Identity). During user registration I need to prompt for the Gender.

I have already inserted the following items in the table.

Code = F
Definition = Female
-------------------
Code = M
Definition = Male

When I generate the DropDownList directly inside GenderDDController and it's view it works perfectly but as soon as I'm in RegisterViewModel I have zero chance due to I can't reference multiple models in Register.cshtml (View).

I should also indicate that I'm new to MVC and I'm sure that's the only reason that I look confused right now.

GenderDD | Model

public partial class GenderDD
{
    public int Id { get; set; }

    [StringLength(1)]
    public string Code { get; set; }

    [StringLength(50)]
    public string Definition { get; set; }

    public DateTime? CreateDate { get; set; }

    [StringLength(50)]
    public string CreatedBy { get; set; }

    public bool Deleted { get; set; }

    public DateTime? UpdateDate { get; set; }

    [StringLength(50)]
    public string UpdatedBy { get; set; }
}

RegisterViewModel | Controller

[AllowAnonymous]
public ActionResult Register()
{
   ViewData["GenderList"] =
   Enum.GetValues(typeof (GenderDD))
     .Cast<GenderDD>()
     .Select(c => new SelectListItem {Text = c.ToString(), Value = c.ToString()});

   return View();
}
3
  • Show your RegisterViewModel Commented Oct 21, 2015 at 9:57
  • 1
    You need to add and populate a SelectList property in RegisterViewModel, then you can use it in the View Commented Oct 21, 2015 at 10:07
  • Edit your question with the code (not in comments) Commented Oct 21, 2015 at 10:12

2 Answers 2

3

You can have multiple classes in your model for Register.cshtml (View)

Simply create a base class such as below and reference it as the Model for that view, this will then give you access to two models.

Public Class RegisterViewModel()
{
    public ModelName ModelName  {get;set;}
    public ModelName2 ModelName2  {get;set;}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this worked perfectly but my next problem is still there where I can't populate DropDownList from GenderDD model.
I think it would help us if you posted the code for your view
2

Use viewbag to pass data as below, You can write in controller

var genderlist = new List<SelectListItem>
{
  new SelectListItem { code = "M", name = "Male" },
  new SelectListItem { code = "F", name = "Female" }                
};
viewbag.genderlist=genderlist;

You can write in view

@Html.DropDownListFor(model => model.gender, new SelectList(ViewBag.genderlist, "code", "name"))

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.