none
How to access data from one Controller to another RRS feed

  • Question

  • I am working on my first app.  I am using Angular for front end and Core 2.1 for backend.  After a month I learned how to send data from front end to the back end.  The data on the controller page is needed in another controller.  How may I access the data on api/stock page from the Stock controller to another Action controller?

    Below is the code for the Stock Controller.

    namespace myBackEnd.Controllers
    {
        [Route("api/stock")]

        [Produces("application/json")]
      
        public class StockController : ControllerBase
        {
            readonly Models.StockContext _context;
            public StockController(Models.StockContext context)
            {

                _context = context;
            }
            [HttpGet]
            public IEnumerable<Models.Stock> Get()
            {
                return _context.Stocks;
            }

            // POST api/values
            [HttpPost]
            public async Task<IActionResult> Post([FromBody]Models.Stock stock)
            {
                _context.Stocks.Add(stock);
                await _context.SaveChangesAsync();
                return Ok(stock);
            }

    Sunday, November 4, 2018 11:08 PM

Answers

  • The first controller persistes the data via its model. The second control reads the persisted data via its model.
    • Marked as answer by DoctorWhoKnew Monday, November 5, 2018 10:07 AM
    Sunday, November 4, 2018 11:42 PM
  • The stock controller needs to persist the data with its model.Then accounts controller simply reads it using its model.

    The models handle data retrival.

    • Marked as answer by DoctorWhoKnew Monday, November 5, 2018 12:01 AM
    Sunday, November 4, 2018 11:51 PM

All replies

  • The first controller persistes the data via its model. The second control reads the persisted data via its model.
    • Marked as answer by DoctorWhoKnew Monday, November 5, 2018 10:07 AM
    Sunday, November 4, 2018 11:42 PM
  • Stefan,  how do I access the api/stock data at the stock controller from a new controller called account controller.  I know this is a very basic question.  I tried googling.  When I use

    [HttpGet] in the accounts controller how to i access the api/stock instead of the default api/accounts?
    Sunday, November 4, 2018 11:49 PM
  • The stock controller needs to persist the data with its model.Then accounts controller simply reads it using its model.

    The models handle data retrival.

    • Marked as answer by DoctorWhoKnew Monday, November 5, 2018 12:01 AM
    Sunday, November 4, 2018 11:51 PM
  • http://forums.asp.net/

    It is where you should post.

    Monday, November 5, 2018 12:43 AM