【发布时间】:2018-12-06 23:54:07
【问题描述】:
我需要一些有关此下拉列表的帮助。我有一个表格,我希望客户的位置成为一个选择项目。我有一个视图模型,可以填充我需要的所有内容,但想让位置可选择。每个客户应该至少有 1 个条目,所以在我看来,在填充下拉列表之前,我可能需要使用“if”语句进行计数,如果只有 1 个,那么只需将其用作 EditorFor。正在从地址表中提取位置,同时从客户表中提取客户名称。
我已经试过了
ViewBag.Locations = new SelectList(db.Addresses, "AddressId", "LocationName");
但是,当前模型设置从地址表中获取,并且该列不是 IEnumerable SelectList。下面是我的模型:
public class AddEventViewModel
{
public static AddEventViewModel GetCustomerInfo(string userId, CustomerEntities db)
{
var QCustInfo = from ad in db.Addresses
join ua in db.UserToAddresses on ad.AddressId equals ua.AddressId
join cus in db.CustomerNames on ad.CustomerId equals cus.CustomerId
where (ua.UserId == userId)
select new AddEventViewModel
{
CustomerId = cus.CustomerId,
CustomerName = cus.CustomerName,
Location = ad.LocationName
};
var result = QCustInfo.SingleOrDefault();
return result;
}
public int CustomerId { get; set; }
[Display(Name = "Location Name")]
public string Location { get; set; }
public bool AllDay { get; set; }
[Display(Name ="Title")]
public string Title { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "End Date")]
public DateTime EndDate { get; set; }
[Display(Name = "Customer Name")]
public string CustomerName { get; set; }
public virtual CustomerNames Customer { get; set; }
public virtual IEnumerable<Addresses> Addresses { get; set; }
表单只是包含字段和作品的基本表单,我只需要选择位置即可。制作另一个 viewModel 似乎不是我当前设置的方式,因为它正在与实际的表模型进行连接。感谢您的帮助。
【问题讨论】:
标签: c# sql-server asp.net-mvc razor-pages