私は、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);
}
はあなたがより良いあなたを置く –
しようとしたものを表示しますクライアント側のコードもここにあります。私たちがさらに助けることができるように。 –
さらに追加! –