我正在尝试使用“注册”页面窗体上的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”拼写字母。如果需要在下拉列表中输入客户名称。
答案 0 :(得分:0)
不是使用Tempdata,而是使用viewbag绑定deopdownlist
这是从项目对象获取数据并将其转换为fabric
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;
当您从控制器传递模型时,我强烈建议键入dropdownfor
@Html.DropDownListFor("CustomerName", ViewBag.ddlcustomer as SelectList, new { @class = "form-control" })