我已经使用代码优先迁移和实体框架6.2将数据存储到数据集中。以下是程序模型的代码。
模型
public class Clients
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
public string address { get; set; }
[Required]
[Phone]
public string contact { get; set; }
[Required]
[ValidationModels.ClientBalValidation]
public double balance { get; set; }
}
public class ClientPayments
{
public int id { get; set; }
public Clients Clients { get; set; }
public int amountPaid { get; set; }
public DateTime datePaid { get; set; }
}
public class ClientPaymentViewModel
{
public IEnumerable<Clients> Client { get; set; }
public ClientPayments ClientPayments { get; set; }
}
然后在控制器中,我编写了以下代码以初始化模型并传递给视图。
控制器
public ActionResult AddPayment()
{
var Clients = _context.TableClients.ToList();
var viewModel = new ClientPaymentViewModel
{
ClientPayments = new ClientPayments { datePaid = DateTime.Now},
Client = Clients
};
return View(viewModel);
}
我所面临的主要问题。在客户列表中,我想显示客户的ID和名称,但只能显示两个(使用此代码的名称)之一。
查看
<div class="form-group">
<label>Client:</label>
@Html.DropDownListFor(m => m.ClientPayments.Clients,new SelectList(Model.Client,"id","name"), new { @class = "form-control" })
</div>