2012-03-22 32 views
0

我很新MVC,但我試圖建立一個兩步用戶註冊系統。我有一個名爲Registration的視圖模型中有一個註冊模型和其他幾個模型。用戶將擁有基於註冊模型的配置文件,然後他們將選擇他們所屬的生產商/分銷商/餐廳/進口商,或創建一個新的。返回的我的ViewModel不驗證或拾取我的下拉列表中的值。他們填充正確,但在帖子中,他們不在vm中。以下是我的視圖/控制器/和模型。我一直在尋找網絡2天沒有運氣。另外,如果您認爲我的註冊方法很古怪,請告訴我。謝謝!MVC 3下拉菜單和多模型ViewModel

控制器:

public class RegistrationController : Controller 
{ 
    private vfContext db = new vfContext(); 
    // 
    // GET: /Registration/ 

    public ActionResult Register() 
    { 
     ViewBag.UserTypeID = new SelectList(db.UserTypes, "UserTypeID", "Name"); 
     ViewBag.ProducerID = new SelectList(db.Producers, "ProducerID", "Name"); 
     ViewBag.PublicationID = new SelectList(db.Publications, "PublicationID", "Name"); 
     ViewBag.ImporterID = new SelectList(db.Importers, "ImporterID", "Name"); 
     ViewBag.DistributorID = new SelectList(db.Distributors, "DistributorID", "Name"); 
     ViewBag.RestaurantID = new SelectList(db.Restaurants, "RestaurantID", "Name"); 

     RegistrationViewModel reg = new RegistrationViewModel(); 

     ViewData.Model = reg; 

     return View("Registration"); 
    } 

    [HttpPost] 
    public ActionResult Register(RegistrationViewModel vm) 
    { 
     if (ModelState.IsValid) 
     { 
      MembershipCreateStatus createStatus; 
      //email is userid 
      Membership.CreateUser(vm.Register.Email, vm.Register.Password, vm.Register.Email, null, null, true, null, out createStatus); 

      if (createStatus == MembershipCreateStatus.Success) 
      { 
       Profile current = Profile.GetProfile(vm.Register.Email); 
       current.FirstName = vm.Register.FirstName; 
       current.LastName = vm.Register.LastName; 
       current.Address1 = vm.Register.Address1; 
       current.Address2 = vm.Register.Address2; 
       current.City = vm.Register.City; 
       current.State = vm.Register.State; 
       current.Postal = vm.Register.Postal; 
       current.UserTypeID = vm.Register.UserTypeID; 

看法 - 我有一個很難複製過來,所以這個問題是的DDL,所以這裏是我如何讓用戶類型ID一個做

@model vf2.Models.RegistrationViewModel 
       <div class="editor-label"> 
       @Html.LabelFor(model => model.Register.UserTypeID, "User Type") 
      </div> 
      <div class="editor-field"> 
       @Html.DropDownList("UserTypeID", String.Empty) 
       @Html.ValidationMessageFor(m => m.Register.UserTypeID) 
      </div> 

型號:

public class RegistrationViewModel 
{ 
    public RegisterModel Register { get; set; } 
    public Producer Producer { get; set; } 
    public Distributor Distributor { get; set; } 
    //public Publication Publication { get; set; } 
    public Restaurant Restaurant { get; set; } 
    public Importer Importer { get; set; } 
} 

這裏是我的註冊模式。

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

    [Required] 
    [Display(Name = "First Name")] 
    public string FirstName { get; set; } 

    [Required] 
    [Display(Name = "Last Name")] 
    public string LastName { get; set; } 

    [Display(Name = "Address")] 
    public string Address1 { get; set; } 
    [Display(Name = "Address Cont.")] 
    public string Address2 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Postal { get; set; } 
    public string Country { get; set; } 

    [DataType(DataType.PhoneNumber)] 
    public string Phone { get; set; } 

    public int ProducerID { get; set; } 
    public int DistributorID { get; set; } 
    public int PublicationID { get; set; } 
    public int ImporterID { get; set; } 
    public int RestaurantID { get; set; } 

    public virtual Producer Producer{ get; set; } 
    public virtual Distributor Distributor { get; set; } 
    public virtual Publication Publication { get; set; } 
    public virtual Importer Importer { get; set; } 
    public virtual Restaurant Restaurant { get; set; } 

    [Required] 
    public int UserTypeID { get; set; } 
    public virtual UserType UserType { get; set; } 

    [Required] 
    [DataType(DataType.EmailAddress)] 
    [Display(Name = "Email address")] 
    public string Email { 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 class Producer 
{ 
    public int ProducerID { get; set; } 

    public string Name { get; set; } 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Postal { get; set; } 
    public string Country { get; set; } 

    [DataType(DataType.PhoneNumber)] 
    public string Phone { get; set; } 

    [DataType(DataType.EmailAddress)] 
    public string Email { get; set; } 

    public string Website { get; set; } 

    public string CreatedBy { get; set; } 
    public DateTime CreatedOn { get; set; } 
    public string UpdatedBy { get; set; } 
    public DateTime? UpdatedOn { get; set; } 
    public Boolean Active { get; set; } 


    public virtual ICollection<Wine> Wines { get; set; } 

} 

回答

0

我想我想通了這一點通過添加以下到我的視圖模型:

 public IEnumerable<UserType> UserTypes { get; set; } 
    public IEnumerable<Producer> Producers { get; set; } 

,然後這對我的看法:

@Html.DropDownListFor(m=>m.Register.UserTypeID,new SelectList(Model.UserTypes,"UserTypeID","Name"),"Select account type")