0

I am not sure how to get my SQL view to actually be read from the .cshtml file. I have my model created to relate to the view and have added the DbSet to the appdbcontext class. I have also done the keyless on the onModelBuilder and created this

public async Task<IActionResult> PreviewEmailAsync()
{
    var myTableResults = _context.WetWeatherReportViews.FromSqlRaw(
        "Select * from dbo.WetWeatherReportView").ToList();
    ViewBag.Data = myTableResults;
    return View();
}

Not sure how to get the view to showcase the information within, I have tried multiple different methods and believe the ViewBag is my closest one, but don't believe I am doing it right. I am unable to get it to work and not sure what I am missing. Thank you for the help!

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • Have you set a breakpoint at `ViewBag.Data = myTableResults`? What's the content of the variable `myTableResults`? How you *move* your data from the controller to the view (by `Model`, `ViewBag` or something else) doesn't matter, just be sure to access the same variable within the view code. – Oliver May 06 '21 at 05:43
  • Do you mean you couldn't get the data from the myTableResults? I suggest you could post the related codes with EF core about how you set the sql view. Besides, I suggest you could refer to this [answer](https://stackoverflow.com/a/52170144/7609093) to know how it works. – Brando Zhang May 06 '21 at 05:54
  • thank you both, going to try to do the break point and look at that answer. – Alberto_Ortiz May 07 '21 at 06:29
  • I think you should share your context makeup to let know why it doesn't return anything. – Brando Zhang May 12 '21 at 02:12
  • I kinda figured it out, I guess it was more an understanding on how to extract the data as @Oliver pointed out. I have this written within `@foreach (WetWeatherReportView l in ViewBag.Data) {` my view to access the data. It is working so far but I am pretty sure I can find a better way to get this to be just a variable I can reference. – Alberto_Ortiz May 13 '21 at 21:34
  • If you have a single (root) object to move from controller to view, you can use the `Model` property. Within your view you can declare at the top `@Model WetWeatherReportView` and whenever you use `Model` variable within your view, you get instant IntelliSense and compiler checks. – Oliver May 14 '21 at 05:43

0 Answers0