Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Send data from the controller to the view using DTO and API in ASP.NET MVC

I have a controller that is sending a list of items to the API and then I'm getting back the item response, it means that I'm going to receive for example if the item was not found or not valid it is working fine!! but I would like to know how can I send the dtoresponse list to a view from my controller and what I need to add in the view?

DTO

 public class ReceivedItemsRequest
    {
        public List<string> Items { get; set; }


    }
    public class ReceivedItemsResponse
    {
        public bool HasErrors { get; set; }

public List<ReceivedItemResponse> ItemResponses { get; set; } = new List<ReceivedItemResponse>() ;

    }

    public class ReceivedItemResponse
    {
        public string Barcode { get; set; }
        public string ErrorMsg { get; set; }


    }
}

Controller


       [HttpPost]
        public async Task<ActionResult> Received(List<string> itemsList)

        {
            try
            {

       var dtoRequest = new ReceivedItemsRequest();
       var dtoResponse = new ReceivedItemsResponse();

       dtoRequest.itemsList= itemsList;
dtoResponse = await API.Post<ReceivedItemsResponse, ReceivedItemsRequest>($"items/", dtoRequest);



    return View(dtoResponse);  ---> this part I'm not sure
 how to send the list and what I need to add in the view or if I need to create a view model?
            
            }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From your code, I discovered you are returning a single record not a List in the controller. So if your API return value is a single record you will send to the View like below:

Your Controller:

[HttpPost]
 public async Task<ActionResult> Received(List<string> itemsList)
 {
    ...
    dtoResponse = await API.Post<ReceivedItemsResponse, ReceivedItemsRequest>($"items/", dtoRequest);
    return View(dtoResponse);
 }

Your View:

@model Application.ReceivedItemsResponse
@{
  ViewBag.Title = "Received Items Response";
}
...HTML Code on View here...

if you are expecting a list as your dtoresponse then on your View you would have:

@model IEnumerable<Application.Models.ReceivedItemsResponse>

@{
     ViewBag.Title = "Index Item Response";
 }
 ...HTML Code Block here ....

Note that your controller does not change as long as you pass either a single record or list but your View does.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!