1

My problem is that I've been following ASP.net MVC/API tutorials I have a working controller in the MVC side of things. For instance this works fine and returns my data in a view:

public class DestinationController : Controller
{
    private ebstestEntities db = new ebstestEntities();

    // GET: Destination
    public async Task<ActionResult> Index()
    {

        return View(await db.CI_DEST_ALL_VARIABLES.ToListAsync());
    }

However when I take a look at the following example, which I had working on another project, I cant seem to work out how to change it to fit in with my project controller above.

// GET: Destination
public async Task<ActionResult> IndexVM()
{
    var model = new BeerIndexVM;

    using (var db = new AngularDemoContext())
    {
        model.Beers = db.Beers.ToList();
    }
    return Json(model, JsonRequestBehavior.AllowGet);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
royG
  • 101
  • 3
  • 13
  • 1
    " I cant seem to work out how to change it to fit in with my project controller above." If you don't describe the problem, we can't help. –  Jan 03 '17 at 20:54
  • 1
    Also, Asp.Net MVC is distinct and separate from Web API. It isn't "ASP.Net MVC Web API". I don't see any Web API related code in the question. –  Jan 03 '17 at 20:56
  • 1
    I would delete that last sentence. Asking for off site resources is a reason to close your question – Marco Jan 03 '17 at 20:56
  • 1
    So in your DestinationController you are returning a `View` and in your second example you are returning `Json`. What happens when you try to return Json instead of a view? – Marco Jan 03 '17 at 20:58
  • 1
    write a title that summarizes the problem, [how to ask question](http://stackoverflow.com/help/how-to-ask) – esiprogrammer Jan 03 '17 at 20:59
  • 1
    your controller is not `ApiController` it's Mvc `Controller`. first of all you need to change ur controller base class to `ApiController` – esiprogrammer Jan 03 '17 at 21:04
  • 1
    @esiprogrammer You can return `Json()` in an MVC controller just as well – Marco Jan 03 '17 at 21:21
  • 1
    @Marco yes you can return Json in MVC controller but the question is about web API, if he's not dealing with fron-end and doesn't have any view so `web api` is the case – esiprogrammer Jan 03 '17 at 21:26
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132236/discussion-between-marco-and-esiprogrammer). – Marco Jan 03 '17 at 21:28
  • Thankyou everyone, apologies for not being clearer about my problem, but it appears that you have all understood where I'm coming from, my trouble lays with my inexperience with asp.net, the framework is absolutely marvelous at scaffolding out projects and there are plenty of tutorials for that, however when tailoring it to my needs I get a little confused. I'm looking at the differences between MVC and Web API controllers more thoroughly as you've suggested, cheers for the comments I really appreciate the help. – royG Jan 04 '17 at 10:00

2 Answers2

2

Because your method signature is async Task<ActionResult>, you need to await a method call. The async version of ToList() is ToListAsync(). So you'll just call that method with the preceding await in front of it giving you await db.Beers.ToListAsync(). Assign that variable to your model, and you should be gold.

Complete method:

// GET: Destination
[HttpGet]
public async Task<ActionResult> IndexVM()
{
    var model = new BeerIndexVM();

    using (var db = new AngularDemoContext())
    {
        model.Beers = await db.Beers.ToListAsync();
    }
    return Json(model, JsonRequestBehavior.AllowGet);
}

I would also add an http verb attribute on the method, although in this particular instance it's not required.

Cameron
  • 2,574
  • 22
  • 37
1

One thing that seems to be missing is your "AcceptVerbs" annotation.

[AcceptVerbs(HttpVerbs.Get)]
public async Task<ActionResult> MethodName()
{
    return json(model, JSonRequestBehavior.AllowGet);
}
Brian Wetherell
  • 65
  • 1
  • 1
  • 6