1

I have data in my apicontroller in following way-

public class OutletPOCController : ApiController
{
    OutletPOCContext db = new OutletPOCContext();

    [System.Web.Http.ActionName("GetTabText")]
    public TabTextModel GetTabText(int bizId)
    {
        var outlet = db.Info.Where(t => t.BizId == bizId).SingleOrDefault();
        return new TabTextModel
        {
            HomeTab = outlet.BizHomeTabText,
            AboutTab = outlet.BizAboutTabText,
            TimingsTab = outlet.BizTimingsTabText,
        };
    }

And now i want to retrieve this data into my view. How shall i create view for this controller and pass the above data? What will be my action method? I am new to webapi and json. Any help is appreciable! Thanks in advance!

4
  • Do you mean that you want to use Javascript to get the data from the client side after the base page is loaded or do you want to get the data in your view and work with it directly? Commented May 27, 2013 at 12:15
  • I want to display the data on to the view that i get in above webapi Commented May 27, 2013 at 12:16
  • 1
    Take a look here codeproject.com/Articles/344078/… for a tutorial. Commented May 27, 2013 at 12:17
  • Thanks a ton.... christiandev solved my issue Commented May 27, 2013 at 13:14

1 Answer 1

0

The API controller dosent really have views in the sense that you create a cshtml page that takes care of how you display your data. The purpose of the ApiController is simply to return data in the format that you want to consume it.

Basically the API exposes raw data to the web, you consume it in some way, and then display it..

I use something similar to this to load data dynamically into a web page. Just a simple web api that returns data to the client.

public class APIController : ApiController
{
    [HttpGet]  
    [HttpPost]  // allow both post and get requests
    public IEnumerable<String> GetData()
    {
        return new List<string>() { "test1", "test2" };
    }
}

When you browse to the API method above it returns this xml data

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <string>test1</string>
  <string>test2</string>
</ArrayOfstring>

Which I get using Jquery and do what I please with (http://api.jquery.com/jQuery.get/):

$.get("/api/GetData", function(data) {
  alert("Data Loaded: " + data);
});

Examples of XML parsing with JS/Jquery:

If you are simply looking to get data into a regular view and work with it there without going through javascript I wouldent use a webapi, but instead get the data in the controller and send it to the view for displaying (ASP MVC4 - Pass List to view via view model).

You can also check out the ViewBag container for passing random odd data to the view https://www.codeproject.com/Articles/476967/What-is-ViewData-ViewBag-and-TempData-MVC-Option-2

On the off chance you really do want to render your data in a view, check this out: Web API - Rendering Razor view by default?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.