0

I need fill some dropdown combo so I create a WebApi and is working OK.

[HttpGet]
[Route("api/States/FindMunicipalityByStateID")]
public IHttpActionResult FindMunicipalityByStateID(String StateID)
{
    List<MunicipalityModel> municipality  = db.municipality.Where(p => p.StateID == StateID)
                       .Select(p => new MunicipalityModel
                       {
                           Name = p.Name,
                           MunicipalityID = p.MunicipalityID
                       })
                       .OrderBy(p => p.Name)
                       .ToList();

    if (municipality == null)
    {
        return NotFound();
    }

    return Ok(municipality);
}

I read it using JQuery.getJSON, and also works fine:

$('#StateID').change(function () {
    var URL = 'http://myServer/myProject/api/States/FindMunicipalityByStateID?StateID=' + $('#StateID').val();
    
    $.getJSON(URL, function (data) {
        var items = '<option>Seleccione Municipio</option>';
        $.each(data, function (i, municipality) {
            items += "<option value='" + municipality.WeekID + "'>" + municipality.Name + "</option>";                
        });
        $('#MunicipalityID').html(items);            
    });
});

The thing is now I want do the same but there is already an ASP.NET MVC controller from my EF wizard for that class so I try to do the same there.

I got 3 errors:

The type or namespace name 'IHttpActionResult' could not be found (are you missing a using directive or an assembly reference?)

The name 'NotFound' does not exist in the current context

The name 'Ok' does not exist in the current context

The first error can be solve with System.Web.Http.IHttpActionResult.

Because can't add using System.Web.Http; because create conflicts with using System.Web.Mvc;

I saw a couple of related question:

Like this IN MVC6 return Json(rows, JsonRequestBehavior.AllowGet) ISSUE but this use a JSonResult

And this one How to return JSON in a ASP.NET MVC/web controller? but this use a Task<ActionResult>

And neither handle the case when the object is not found.

Questions:

  • Is there a reason WebApi controller doesn't need convert the result to JSON but an ASP.NET MVC controller does? Because my jQuery can read it just fine.

  • Can I convert my WebApi code to ASP.NET MVC or need to use one of the examples?

  • Do I need the Not Found result ?

Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • 4
    mvc and web-api are 2 different things. If you want to return json in mvc, then you need to return a `JsonResult` (i.e `return Json(municipality, JsonRequestBehavior.AllowGet);`) –  Apr 10 '18 at 20:41
  • @StephenMuecke Well I don't want return a JsonResult just make it work. I assume because Im using `jQuery.getJson()` I need a Json. But didn't need anything speciall for WebApi work with the getJson. Also according to [**this**](https://stackoverflow.com/questions/32353866/what-is-the-difference-between-mvc-controller-and-web-api-controller-in-asp-net) now mvc and web-api are the same on mvc 6 – Juan Carlos Oropeza Apr 10 '18 at 20:47
  • You have not tagged this asp.net-core-mvc (the correct name for mvc6) - is that what you are using? –  Apr 10 '18 at 20:48
  • @StephenMuecke Ok I confess ignorance there. I always call it asp.net-mvc. Didnt know they change the name :(. Im using VS 2017 with all new packages. I assume is using mvc6. How can be sure? – Juan Carlos Oropeza Apr 10 '18 at 20:50
  • @StephenMuecke mmm look like Im using mvc 5? ` ` – Juan Carlos Oropeza Apr 10 '18 at 20:53
  • 1
    In that case, you must use `return Json(..);` –  Apr 10 '18 at 20:53
  • possible duplicate of https://stackoverflow.com/questions/35443521/asp-net-mvc-core-how-can-i-return-a-json-query – user9405863 Apr 10 '18 at 21:05
  • @StephenMuecke Well I will try to migrate to mvc 6. My fault by assuming that would be the default one :( – Juan Carlos Oropeza Apr 10 '18 at 21:20

0 Answers0