Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Cosmos DB数据在寄存器页面C#MVC上填充下拉值?_C#_Html_Asp.net Mvc_Azure Cosmosdb - Fatal编程技术网

如何使用Cosmos DB数据在寄存器页面C#MVC上填充下拉值?

如何使用Cosmos DB数据在寄存器页面C#MVC上填充下拉值?,c#,html,asp.net-mvc,azure-cosmosdb,C#,Html,Asp.net Mvc,Azure Cosmosdb,我正在尝试在注册页面表单上使用Azure Cosmos db显示下拉列表。但它没有显示任何东西。我是C#MVC的新手 我已经创建了单独的模型Customer来从Azure获取客户数据。使用Tempdata将其传递给视图。但它只是在下拉列表中显示单词“CustomerName”的字母。请帮忙,我是C的新手# namespace WebApplication1.Models { 公共类客户 { [JsonProperty(PropertyName=“id”)] 公共字符串Id{get;set;} [

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

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

namespace WebApplication1.Models
{
公共类客户
{
[JsonProperty(PropertyName=“id”)]
公共字符串Id{get;set;}
[JsonProperty(PropertyName=“Name”)]
公共字符串名称{get;set;}
[JsonProperty(PropertyName=“GroupId”)]
public int GroupId{get;set;}
}
}   
公共类RegisterViewModel
{
[必需]
[电邮地址]
[显示(Name=“电子邮件”)]
公共字符串电子邮件{get;set;}
[必需]
[StringLength(100,ErrorMessage={0}的长度必须至少为{2}个字符。”,MinimumLength=6)]
[数据类型(数据类型.密码)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[数据类型(数据类型.密码)]
[显示(Name=“确认密码”)]
[比较(“密码”,ErrorMessage=“密码和确认密码不匹配。”)]
公共字符串ConfirmPassword{get;set;}
[必需]
[显示(Name=“GroupId”)]
public int GroupId{get;set;}
公共客户客户名称{get;set;}
}
公共异步任务);
返回重定向操作(“着陆”、“设备”);
}
加法器(结果);
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}
@模型WebApplication1.Models.RegisterViewModel
@使用WebApplication1.模型;
@使用制度;
@{
ViewBag.Title=“寄存器”;
}
@ViewBag.Title。
@{
var customerdata=(IEnumerable)TempData[“客户”];
}
@使用(Html.BeginForm(“Register”、“Account”、FormMethod.Post、new{@class=“form horizontal”、role=“form”}))
{
@Html.AntiForgeryToken()
创建一个新帐户。

@Html.ValidationSummary(“,new{@class=“text danger”}) @DropDownList(“CustomerName”,新的选择列表(nameof(Model.CustomerName)),新的{@class=“formcontrol”}) @LabelFor(m=>m.Email,新的{@class=“col-md-2控制标签”}) @TextBoxFor(m=>m.Email,新的{@class=“form control”}) @LabelFor(m=>m.Password,新的{@class=“col-md-2控制标签”}) @Html.PasswordFor(m=>m.Password,新的{@class=“form control”}) @LabelFor(m=>m.ConfirmPassword,新的{@class=“col-md-2控制标签”}) @Html.PasswordFor(m=>m.ConfirmPassword,new{@class=“form control”}) @LabelFor(m=>m.GroupId,新的{@class=“col-md-2控制标签”}) @TextBoxFor(m=>m.GroupId,新的{@class=“form control”}) }

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

使用viewbag绑定deopdownlist而不是使用Tempdata

下面是从item对象获取数据并将其转换为
List

我建议您在从控制器传递模型时强烈键入dropdownfor

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

获取以下错误:没有键为“CustomerName”的“IEnumerable”类型的ViewData项。@MannanBahelimI发现问题:ViewBag.ddlcustomer为空。但我不知道如何修复它。请使用model=>model.CustomerName.groupId重命名您的ddl。您的意思是使用:
DownListFor吗(model=>model.CustomerName.GroupId,ViewBag.ddlcustomer作为SelectList,new{@class=“form control”})
它给了我同样的错误@MannanBahelim
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" })
  @Html.DropDownListFor(model=>model.CustomerName.GroupId, ViewBag.ddlcustomer as SelectList, new { @class = "form-control" })