Populating dropdown from SQL Server with ajax in C# and ASP.NET MVC

It seem that debugger doesnt enter at all at the success function. The dropdown im trying to populate is part of a modal in /Dokument/Dokument route when i press a button i get a modal with some fields. One of the fields is a dropdown which has to get values from Tipi table. In the dropdown I see nothing not even the message

Route config

namespace Archieve1
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
            );
        }
    }
}

AJAX script

script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: "/NgarkoDokumentController/GetTipi",
            data: "{}",
            success: function (data) {
                var s = '<option value="-1">Selektoni Tipin</option>';
                for (var i = 0; i < data.length; i++) {
                    s += '<option value="' + data[i].Id_Tipi + '">' + data[i].Emri_llojit + '</option>';
                }
                $("#tipiDropdown").html(s);
            }
        });
    });
</script>

Controller method

 using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace Archieve1.Controllers
{
    public class NgarkoDokumentController : Controller
    {

        public ActionResult Dokument()
        {
             return View();
        }
        // GET: NgarkoDokument

            
        public ActionResult GetTipi()
        {
            Test_kristiEntities db = new Test_kristiEntities();
            return Json(db.Tipi.Select(x => new
            {
                Id_Tipi = x.Id_Tipi,
                Emri_llojit = x.Emri_llojit
            }).ToList(), JsonRequestBehavior.AllowGet);

            // return View();
        }
    }
}

Model I built with some tables

namespace Archieve1.Models
{
    public class NgarkoDokument
    {
        public Dokumenti Dokumenti { get; set; }
        public Fusha_Indeksimit FushaIndeksimit { get; set; }
        public Vendndodhja_Fizike Vendndodhja_fizike { get; set; }
        public IEnumerable<Tipi> Tipi { get; set; }  
    }
}

And here is the html

@model Archieve1.Models.NgarkoDokument
    <select title="Lloji i dokumentit" name="lloji" class="form-control col-md-3 box" id="tipiDropdown"> </select>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The default route of ASP.NET MVC 5 {controller}/{action}/{id}, and the /{id} part is optional.

{controller} refers to the class name of your C# controller, but it removes the word Controller at the end. In your example, the controller name would be NgarkoDokument.

{action} refers to the method name of your action. In your example, GetTipi.

So the route becomes /NgarkoDokument/GetTipi.

Here’s the relevant documentation:


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x