【问题标题】:Use a view model to pass two models into a view in ASP.NET Core使用视图模型将两个模型传递到 ASP.NET Core 中的视图中
【发布时间】: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


【解决方案1】:

您可以创建一个包装两个模型的ViewModelDTO,然后在视图中使用ViewModelDTO

ViewModelDTO.cs:

public class ViewModelDTO
{
    public List<Customer> Customer { get; set; }  
    public ReqRFQ ReqRFQ { get; set; }
}

控制器:

public async Task<IActionResult> IndexMethod()
{
    ViewModelDTO viewModelDTO = new ViewModelDTO();
    viewModelDTO.ReqRFQ = new ReqRFQ();
    viewModelDTO.Customer = new List<Customer>();

    viewModelDTO.ReqRFQ.ReqRFQId = 0;
    viewModelDTO.ReqRFQ.RequisitionId = 1;
    viewModelDTO.ReqRFQ.RequisitionNo = Guid.Empty;
    viewModelDTO.ReqRFQ.RequisitionTitle = "";

    viewModelDTO.Customer = _context.Customer.ToList();
    return View(viewModelDTO);
}

然后在视图中使用ViewModelDTO

@model Entities.Models.ViewModelDTO

这样,您可以在同一页面上使用来自两个模型的数据。

希望这可以帮到你。

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 2017-10-04
    • 2022-10-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多