0

I am relatively new to MVC, and still confused, therefore I need help with a problem.

I have created a web scraper console app where I compare two articles(where I fetch the data from a certain API), now I want to make this an MVC project where I have two dropdown lists that need to be filled with data from the above-mentioned API so that I can compare the two articles.

Unfortunately for me, I do not know how to populate these drop downs, that is, I do not know what logic goes in the controller and the model... Can someone give me a hint or recommend a good read because I am totally lost.

Thanks in advance!

Edit: Something like this: MVC app

1

1 Answer 1

1

One way to populate your dropdown lists is by using ViewData.

Let's assume that your calls to the API reside in a separate service. The API would need to return a List. In this example the List will be of a custom class: List<CustomClass>. Let's assume that your custom class contains a property Id, and a property Name. Your controller would look like:

public class HomeController : Controller
{
    private readonly IApiService _apiService;

    public HomeController(
        IApiService apiService)
    {
        _apiService = apiService;
    }

    public IActionResult Index()
    {
        // Get the data from the API into the ViewData
        // In this example, Id will be the Id of the dropdown option,
        // and the Name will be what's displayed to the user

        ViewData["DataFromArticle1"] = new SelectList(
                await _apiService.GetDataFromArticle1Async(), 
                "Id", "Name");

        ViewData["DataFromArticle2"] = new SelectList(
                await _apiService.GetDataFromArticle2Async(), 
                "Id", "Name");

        return View();
    }
}

Now, to populate the dropdowns in your View:

<select asp-items="ViewBag.DataFromArticle1"></select>
<select asp-items="ViewBag.DataFromArticle2"></select>

UPDATE

The following code will call your API endpoint by AJAX. We assume that you have created a WebApi, and that within your WebAPI you have an ArticleDataController with a method called GetDataFromArticle1.

Your view:

<select id="dataFromArticle1"></select>

Your JavaScript:

$(document).ready(function () {  
    $.ajax({  
        type: "GET",  
        url: "/api/ArticleData/GetDataFromArticle1",   
        success: function (data) {  
            var s = '<option value="-1">Please Select a Department</option>';  
            for (var i = 0; i < data.length; i++) {  
                s += '<option value="' + data[i].Id+ '">' + data[i].Name + '</option>';  
            }  
            $("#dataFromArticle1").html(s);  
        }  
    });  
});  
Sign up to request clarification or add additional context in comments.

11 Comments

I updated my question with an image of what the app should do. Now I have two questions: The result of the compared articles should come from the API also. Do I need to make two separate web services, or should I put the entire code from the console app in the web service?
In your question, you mention that you already have an API which your console app calls to (1) get the data, and (2) compare it. In my example, the ApiService will take care of calling your API for your different tasks (i.e. getting the data, comparing the data, etc.) I don't know how your console app calls the API, but my recommendation is that you should place the code that calls your API within the ApiService. The idea here is to separate concerns within your project (your Controller does not care how you get the data or how you compare it, it only cares that the data is there).
Thank you for your explanation, some things have become much clearer, I will follow your instructions and let you know how it goes. Thanks again!
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
Can I solve this problem by using ajax call instead?
|

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.