Home > Software design >  Read Data from table with no relationship
Read Data from table with no relationship

Time:01-03

I have some global information (Company name, street, phone, …) stored in a table "CompInfo". This table has only one entry and has no relationship to other tables.

Now I need this information in a view to generate, i.e. offers. I tried to add this data in the controller, but I don't know how I can send this info to my view.

Here is my controller:

public async Task<IActionResult> Offer(int qry_offer)
{
    ViewData["Message"] = "Offer";


    var x412Context = _context.Offer
        .Where(m => m.Id == qry_offer);


    x412Context = x412Context
        .Include(o => o.Kunde)
        .Include(o => o.Mandant)
        .Include(m => m.OfferPos)
            .ThenInclude(m => m.Ust)
        ;
    return View(await x412Context.ToListAsync());
}

First I add the following code

var Comp_Info = _context.CompInfo
    .Where(m => m.Id == 1);

With breakpoints and debugging I see that Comp_Info has the information I require. But I don´t know how to get this info to my view.

I tried

ViewData["street"] = Comp_Info.street;

But this doesn't work.

Does anyone have an idea how I can transmit the data to the view

CodePudding user response:

you can implement another class which should have members as the list (of the same type x412Context.ToListAsync()) and another member which will have the same datatype as Comp_Info.street.

then you can return the same class object to the view like return View(newClassObj);, you can access it as we do to access model class members in view.

CodePudding user response:

You can return a viewmodel in your Offer method that looks like this :

public class FooVM
{
  public List<Offer> Offers {get; set;}
  public CompInfo CompInfo{get; set;}
}

and in your controller initilize the FooVm like this:

var vm = new FooVM 
{
  Offers = await x412Context.ToListAsync(),
  CompInfo =  Comp_Info 
};

and then in your Offer Method you return View(vm);

then your Offer.cshtml will look something like :

@model FooVm;
// some html code 
@foreach (var offer in Model.Offers)
{
// Do something
}
<span>@Model.CompInfo.Street</span>
  • Related