-1

I have a List<string> stored in local storage and I'm getting those values using javascript and sending it to a POST method of my controller using an AJAX call.

In the controller of the POST method an API is called in a for loop to fetch details in JSON format. After deserializing, it is getting stored in a List of model and this model needs to be sent to the view to display the list of data.

The model data needs to be passed to the view, but it is not happening and I am not sure where I am going wrong.

Model:

public class Fruit { [JsonProperty("name")] public string FruitName { get; set; } [JsonProperty("image")] public string ImageUrl { get; set; } } public class FruitDataModel { public List<Fruit> FruitData { get; set; } } 

controller:

[HttpGet] public ActionResult FruitList() { return View(); } [Route("FruitController/FruitList")][HttpPost] public async Task < ActionResult > FruitList(List<string> fruitItems) { var items = fruitItems; List<Fruit> results = new List <Fruit>(); foreach(var item in items) { var result = await CallApiAsync(item); //method returns the List of Fruit data results.Add(result); } var model = new FruitDataModel { FruitData = results }; return View("~/Views/FruitList", model); // where the model has the data inside it. } View: @model Fruit.Models.FruitDataModel@ { ViewBag.Title = "FruitList"; var index = 0; } @if (Model != null) { foreach(var item in FruitData) { <li>@item.FruitName</li> } } 

JS to send the local storage values to the post controller method:

// I am using jQuery 3.3.1 const value = JSON.parse(localStorage.getItem('fruitItems')) || []; console.log(value); $.ajax({ url: '@Url.Action("FruitList", "Fruit")', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(value), // Send localStorage value dataType: 'json', success: function (response) { console.log('Data sent successfully'); }, error: function (xhr, status, error) { console.error('Error sending data: ', error); } }); 
2
  • 2
    The code shown appears to be mixing up client-side code and server-side code, so you may also be mixing the two in weird ways. But at least at a glance, the only thing you do in response to the AJAX operation is this: console.log('Data sent successfully'); Is that happening successfully? Where do you try to use the data returned from the server in any way? It also appears that the server-side code is returning HTML, but the client-side code is expecting JSON? Can you clarify?
    – David
    CommentedSep 18, 2024 at 12:23
  • You are expecting a JSON result, but what you returned is return View("~/Views/FruitList", model);, this is the issue... You should return model instead. And change public async Task < ActionResult > FruitList to public async Task < List<Fruit> > FruitList
    – Tiny Wang
    CommentedSep 18, 2024 at 13:11

1 Answer 1

0

You are expecting a JSON result, but what you returned is return View("~/Views/FruitList", model);, This is the issue...

You should return model instead. Try codes below:

[Route("FruitController/FruitList")][HttpPost] public async Task <List<Fruit>> FruitList(List<string> fruitItems) { var items = fruitItems; List<Fruit> results = new List <Fruit>(); foreach(var item in items) { var result = await CallApiAsync(item); //method returns the List of Fruit data results.Add(result); } var model = new FruitDataModel { FruitData = results }; return model; // where the model has the data inside it. } 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.