我正在尝试在注册页面表单上使用 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/