简体   繁体   中英

How to use a multiple models / contexts (routed by Id) in a View?

This is the code from the View

@model BalanceSheets;
<tr class="row100 body">
       <td class="cell100 column1"><bold>@Html.DisplayNameFor(model => model.BalanceSheet2019.Inventories): </bold></td>
       <td class="cell100 column2">@string.Format(KiloFormat(Convert.ToInt64(Model.BalanceSheet2019.Inventories)), Model.BalanceSheet2019.Inventories)</td>
       <td class="cell100 column2">@string.Format(KiloFormat(Convert.ToInt64(Model.BalanceSheet2018.Inventories)), Model.BalanceSheet2018.Inventories)</td>
       <td class="cell100 column2">@string.Format(KiloFormat(Convert.ToInt64(Model.BalanceSheet2017.Inventories)), Model.BalanceSheet2017.Inventories)</td>
    </tr>

This is the BalanceSheets.cs Model

public class BalanceSheets
{
    public BalanceSheet2019 BalanceSheet2019 { get; set; }
    public BalanceSheet2018 BalanceSheet2018 { get; set; }
    public BalanceSheet2017 BalanceSheet2017 { get; set; }
}

This is the BalanceSheets2019/18/17.cs code

public class BalanceSheet2019
{
    [Required]
    [Key]
    [Display(Name = "ID")]
    public int Id { get; set; } //1

    [Display(Name = "Cash and cash equivalents")] //4
    public double Cash_and_cash_equivalents{ get; set; }

    [Display(Name = "Short-term investments")] //4
    public double Short_term_investments { get; set; }

    .....

}

This is the code from the BalanceSheetsController

public async Task<IActionResult> BalanceSheet(string Id)
    {
        if (Id == null)
        {
            return NotFound();
        }

        var balanceSheets = await _bscontext.BalanceSheet2019
            .FirstOrDefaultAsync(m => m.Id.ToString() == Id);

        if (balanceSheets == null)
        {
            return NotFound();
        }

        return View(balanceSheets);
    }

Now I have all of the following bdcontexts and I don't know how to return them properly to the View by ID

    private readonly BalanceSheets2019Context _bscontext;

    private readonly BalanceSheets2018Context _bscontext8;

    private readonly BalanceSheets2017Context _bscontext7;

By using that code I get the following error probably because I am using "var balanceSheets = await _bscontext.BalanceSheet2019" instead of something else -> An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Project.Models.BalanceSheet2019', but this ViewDataDictionary instance requires a model item of type 'Project.Models.BalanceSheets'. Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)

Unless I'm missing something, you can just new up your model, hydrate it and return it:

var model = new BalanceSheets();
model.BalanceSheet2019 = await _bscontext.BalanceSheet2019
            .FirstOrDefaultAsync(m => m.Id.ToString() == Id);
model.BalanceSheet2018 = await _bscontext8.BalanceSheet2018
            .FirstOrDefaultAsync(m => m.Id.ToString() == Id);
model.BalanceSheet2017 = await _bscontext7.BalanceSheet2017
            .FirstOrDefaultAsync(m => m.Id.ToString() == Id);    

return View(model);

I had to guess on some of that, without seeing more of your entities and db contexts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM