2016-05-04 9 views
0

私は、ASP.NETでユーザーが登録することができ、(とりわけ)携帯電話番号を選択できるシステムを作成しています細胞担体を含む。私は、MVC 5を使用してMVCテンプレート内の個々のユーザーアカウントを使用しています。私は、「キャリア」と呼ばれる別のモデルを持っています。モデルの仕様は、それ自身のファイルにCarrierModels.csです:ユーザー登録ビュー(ASP.NET MVC5)で別のモデルのアイテムのドロップダウンビューを追加する

public class Carrier 
{ 
    public string name { get; set; } 
    public int id { get; set; } 
    public string gateway { get; set; } 
} 

私は、ユーザーがキャリアのセットから選択できるようになるユーザー登録(作成を扱う別のコントローラがあるのドロップダウンを取得しようとしていますキャリアの変更など)。私はAccountViewModelsのRegisterViewModelクラスを変更し、LINQを使ってリストから項目を引き出そうとしましたが、それは引き続きNull参照例外を返します。このドロップダウンメニューを登録ページに入れるにはどうすればいいですか?

編集2:(それが更新年代にフォームを)もっとコードを表示:

ここ

がコードの表示では、全体的に、DropDownListForラインはNullReferenceExceptionsを引き起こすものです。

<div class="form-group"> 
    @Html.LabelFor(m => m.carrierListIndex, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.DropDownListFor(m=>m.carrierListIndex, Model.carrierList.Select(Text = x.name, Value = x.Id.ToString(), Selected = Model.CarrierListIndex == x.Id), "Please Select...",null) 
      </div> 
</div> 

これはキャリアに対応RegisterViewModelの一部です:DropDownListFor()の第二引数はIenumerable<SelectListItem>ある

[AllowAnonymous] 
    public ActionResult Register() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      ApplicationUser user = new ApplicationUser { UserName = model.Email, Email = model.Email, studentID = model.studentID, phone=model.phone, carrierList=model.carrierList }; 
      var result = await UserManager.CreateAsync(user, model.Password); 

      var context = new ApplicationDbContext(); 
      var roleStore = new RoleStore<IdentityRole>(context); 
      var roleManager = new RoleManager<IdentityRole>(roleStore); 

      var userStore = new UserStore<ApplicationUser>(context); 
      var userManager = new UserManager<ApplicationUser>(userStore); 



      if (result.Succeeded) 
      { 
       userManager.AddToRole(user.Id, "User"); 
       await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

       // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
       // Send an email with this link 
       // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
       // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
       // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

       return RedirectToAction("Index", "Home"); 
      } 
      AddErrors(result); 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 
+0

はあなたがより良いあなたを置く –

+0

しようとしたものを表示しますクライアント側のコードもここにあります。私たちがさらに助けることができるように。 –

+0

さらに追加! –

答えて

0

います:AccountController.csで

[Display(Name = "Wireless Carrier")] 
public int carrierListIndex { get; set; } 
public static db db = new db(); 
public List<Carrier> carrierList = db.Carriers.ToList(); 

マイ2つのレジスタの機能匿名のオブジェクトを渡すだけです。

public class RegisterViewModel 
{ 
    .... 
    [Display(Name = "Wireless Carrier")] 
    [Required(ErrorMessage = "Please select a carrier")] 
    public int SelectedCarrier { get; set; } 
    public IEnumerable<SelectListItem> CarrierList { get; set; } 
} 

にビューモデルを変更し、ビューにモデルを渡すGETメソッドで

[AllowAnonymous] 
public ActionResult Register() 
{ 
    RegisterViewModel model = new RegisterViewModel(); 
    ConfigureViewModel(model); 
    return View(model); 
} 

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Register(RegisterViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     ConfigureViewModel(model); // must reassign the SelectList 
     return View(model); 
    } 
    .... // save and redirect 
} 

private ConfigureViewModel(RegisterViewModel model) 
{ 
    model.carrierList = new SelectList(db.Carriers, "Id", "name"); 
} 

とビューで

@model RegisterViewModel 
.... 
@Html.DropDownListFor(m => m.SelectedCarrier, Model.CarrierList,"Please select") 
@Html.ValidationMessageFor(m => m.SelectedCarrier) 
関連する問題