【发布时间】:2022-09-27 10:04:59
【问题描述】:
我正在尝试使用视图模型将两个模型传递到视图中。另一个模型已经有我试图在表格中显示的数据。
我想在同一页面上发布另一个表格。当我加载页面时,我收到一条错误消息,提示我尝试将Customer 列表而不是Entities.Models.RQF 传递到视图中。
public partial class Customer
{
public Customer()
{
//Supplier = new HashSet<Supplier>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[Required]
[MinLength(2)]
public string FirstName { get; set; }
public string LastName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string PhoneNumber { get; set; }
public string Company { get; set; }
}
public partial class ReqRFQ
{
public ReqRFQ()
{
//Supplier = new HashSet<Supplier>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ReqRFQId { get; set; }
public int? RequisitionId { get; set; }
public Guid RequisitionNo { get; set; }
public string RequisitionTitle{ get; set; }
public List<Customer> Customer { get; set; }
[NotMapped]
public IFormFile FormFile { get; set; }
}
HTML 标记:
@model Entities.Models.ReqRFQ
@{
ViewData[\"Title\"] = \"Home Page\"; }
var Req = (List<Customer>)ViewData[\"Cust\"];
Layout = null;
}
<table class=\"table table-bordered\" id=\"customerTable\">
<thead>
<tr>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Email
</th>
<th>
PhoneNumber
</th>
<th>
Company
</th>
<th>
<a onclick=\"showInPopup(\'@Url.Action(\"AddOrEdit\",\"RFQ\",null,Context.Request.Scheme)\',\'New Transaction\')\" class=\"btn btn-success text-white\"><i class=\"fas fa-random\"></i> New Transaction</a>
</th>
</tr>
</thead>
<tbody>
@foreach (var Req in Model.Customer)
{
<tr>
<td>
Req.FirstName
</td>
<td>
Req.LastName
</td>
<td>
Req.Email
</td>
<td>
Req.PhoneNumber
</td>
<td>
Req.Company
</td>
<td>
<a onclick=\"showInPopup(\'@Url.Action(\"AddOrEdit\", \"RFQ\", new { id = @Req.Customer.}, Context.Request.Scheme)\',\'Update Transaction\')\" class=\"btn btn-info text-white\"><i class=\"fas fa-pencil-alt\"></i> Edit</a>
<form asp-action=\"Delete\" asp-route-id=\"Req.Id\" onsubmit=\"return jQueryAjaxDelete(this)\" class=\"d-inline\">
<input type=\"hidden\" asp-for=\"Req.Id\" />
<input type=\"submit\" value=\"Delete\" class=\"btn btn-danger\" />
</form>
</td>
</tr>
}}
</tbody>
</table>
C#代码:
public async Task<IActionResult> IndexMethod()
{
Customers = await _customer.GetAllAsync();
ReqRFQ viewModel = new ReqRFQ();
viewModel.ReqRFQId = 0;
viewModel.RequisitionId = 1;
viewModel.RequisitionNo = Guid.Empty;
viewModel.RequisitionTitle = \"\";
viewModel.Status = \"\";
viewModel.Customer = new List<Customer>();
var customers = _context.Customer.ToList();
foreach (Customer customer in customers)
{
var eachCustomer = new Customer();
eachCustomer.Id = customer.Id;
eachCustomer.FirstName = customer.FirstName;
eachCustomer.LastName = customer.LastName;
eachCustomer.Email = customer.Email;
eachCustomer.PhoneNumber = customer.PhoneNumber;
eachCustomer.Company = customer.Company;
viewModel.Customer.Add(eachCustomer);
}
// viewModel.Add(viewModel)
// ViewData[\"Cust\"] = Customers.ToList();
// ViewData[\"BEELevelId\"] = new SelectList(_context.BEELevel, \"BEELevelId\", \"BEE_LevelId\");
return View(viewModel);
}
-
您也可以粘贴控制器代码吗?
-
@HighlanderGrog 已编辑
-
看起来你已经尝试了一些东西——你尝试过 ViewData[\"Cust\"] = viewModel.Customer; ?
标签: c# asp.net-core-mvc razor-pages