c# - 如何在注册页面 C# MVC 上使用 Cosmos DB 数据填充下拉值?

标签 c# html asp.net-mvc azure-cosmosdb

我正在尝试在注册页面表单上使用 Azure Cosmos db 显示下拉列表。但它没有显示任何东西。我是 C# MVC 的新手

我创建了单独的模型 Customer 以从 Azure 获取客户数据。使用 Tempdata 将其传递给 View。但它只是在下拉列表中显示单词“CustomerName”字母。请帮忙,我是 C# 的新手

namespace WebApplication1.Models
{

    public class Customer
    {

        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }


        [JsonProperty(PropertyName = "Name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "GroupId")]
        public int GroupId { get; set; }

    }
  }   


public class RegisterViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        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; }

        [Required]
        [Display(Name = "GroupId")]
        public int GroupId { get; set; }

        public Customer CustomerName { get; set; }
           }


public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, GroupId = model.GroupId};
                var item = await DocumentDBRepository<Customer>.GetCustomerAsync(d => d.Id != null);
               TempData["item"] = item.ToList();
                ViewBag.Id = item.Select(d =>d.GroupId);
                ViewBag.Name = item.Select(d => d.Name);

                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    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("Landing", "Device");
                }
                AddErrors(result);
            }

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





@model WebApplication1.Models.RegisterViewModel
@using WebApplication1.Models;
@using System;
@{
    ViewBag.Title = "Register";
}

<h2>@ViewBag.Title.</h2>

@{
    var customerdata = (IEnumerable<Customer>)TempData["Customer"];
}

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })

    <div class="form-group">

        <div class="col-md-10">
            @Html.DropDownList("CustomerName", new SelectList(nameof(Model.CustomerName)), new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.GroupId, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.GroupId, new { @class = "form-control" })
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>
}

我没有收到任何错误。但也没有获得所需的结果。下拉列表中显示拼写为“CustomerName”的字母。我需要在下拉列表中显示客户名称的地方。

最佳答案

而不是使用 Tempdata 使用 viewbag 来绑定(bind) deopdownlist

这是从项目对象中获取数据并将其转换为 List<SelectListItem> 的代码

var item = await DocumentDBRepository<Customer>.GetCustomerAsync(d => d.Id != null);
               TempData["item"] = item.ToList();
                ViewBag.Id = item.Select(d =>d.GroupId);
                ViewBag.Name = item.Select(d => d.Name);

                List<SelectListItem> ddlcustomer = (from c in item
                        select new SelectListItem
                        {
                            Text = c.Name,
                            Value = c.GroupId,
                        }).ToList();

                        ViewBag.ddlcustomer = ddlcustomer;

查看页面

@Html.DropDownListFor("CustomerName", ViewBag.ddlcustomer as SelectList, new { @class = "form-control" })

我建议您在从 Controller 传递模型时强烈输入 dropdownfor

  @Html.DropDownListFor(model=>model.CustomerName.GroupId, ViewBag.ddlcustomer as SelectList, new { @class = "form-control" })

关于c# - 如何在注册页面 C# MVC 上使用 Cosmos DB 数据填充下拉值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56161908/

相关文章:

c# - 相同的代码在不同的机器上表现不同 - 可能是什么原因? (CLR 版本问题?)

c# - 如何在命令行中确定 C# 编译器版本

jquery - 有没有一个好的方法来处理不显眼的验证看似异步的行为?

html - 悬停时如何使用标题标签而不弹出标题?

asp.net-mvc - 在 ASP.NET MVC 5 中使用 Entity Framework 中的存储过程从多个表中获取多条记录

jquery - ASP.NET MVC View 重新渲染其自身的一部分

c# - Azure Function App - 引用外部库

javascript - 使用 UmbracoV7 中的选择框更改每页的项目数

Javascript,替换类里面句子中的第一个单词

html - 两个 div 重叠以及如何获得 div 的自动高度