繁体   English   中英

ASP.NET MVC 5 RegisterView - 如何实现一个下拉列表

[英]ASP.NET MVC 5 RegisterView - how to implement a Dropdown list

想要在 RegisterView 上实现一个下拉列表,不知道该怎么做我是 asp.net mvc5 和以下代码优先方法的新手。 我已经有一张办公室的桌子。 我将列出我所有的编码。 我认为(我不确定)我必须初始化列表,但不知道该怎么做以及放在哪里

有一个model Office.Cs

    public class Office
    {
        public byte Id { get; set; }
        public string Name { get; set; }
    }
}

身份 Model

        public IEnumerable<Office> Office { get; set; }

        [Required]
        public byte OfficeId { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

AccountViewModel.cs

public class RegisterViewModel
    {
        public OfficeViewModel OfficeViewModel { get; set; }
                     
        [Required]
        [Display(Name = "Office")]
        public byte OfficeId { get; set; }

AccountController.cs

            if (ModelState.IsValid)
            {
                
                
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, OfficeId= model.OfficeId};
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    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);

注册视图.cshtml

        <div class="form-group">
            @Html.LabelFor(m => m.OfficeId)
            @Html.DropDownListFor(m => m.OfficeId, new SelectList(Model.Office, "Id", "Name"), "Select", new { @class = "form-control" })
        </div>

在你的 Controller

List<SelectListItem> li = new List<SelectListItem>();
// add you data to the list
var query = from of in your_context.Office
            select new SelectListItem()
            {
                  Text = of.Name,
                  Value = of.Id
            };
li = query.ToList();
ViewBag.Office = li;

在视图中

<div class="col-md-10">
   @Html.DropDownList("Office", ViewBag.Office as List<SelectListItem>, new { @class = "form-control" })
</div>

您需要在应在 ViewModel class 中声明的SelectList类型字段中填充数据。 SelectList类型集合的该字段将在View中用于与下拉列表绑定。

It is explained in my answer of another post: MVC C# Dropdown list Showing System.Web.SelectListItem on the model and can not blind to controller

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM