Home > DeveloperSection > Forums > Dynamically populate the drop-down using jQuery in ASP.Net MVC3
sandy taylor
sandy taylor

Total Post:46

Points:324
Posted on    February-11-2015 7:06 AM

 .NET .NET  JQuery  Mvc3 
Ratings:


 1 Reply(s)
 529  View(s)
Rate this:
I have a model
public class City
{
    public string Name { get; set; }
    public int ID { get; set; }
}



Anurag Sharma
Anurag Sharma

Total Post:40

Points:280
Posted on    February-11-2015 7:11 AM

In your controller:

[HttpGet]
    public virtual JsonResult LoadInfo()
    {
        var query = _repository.GetInformation(); //Here you return the data. 
        return Json(query, JsonRequestBehavior.AllowGet);
    }

Then in your view:

<select id="info"></select>
Then you load the drop down using jQuery
function LoadInfo() {
    $.getJSON("@Url.Action(MVC.ControllerName.MethodName())", null,
        function (data) {
            $("#info").empty();
            $.each(data, function () {
                $("#info").append($("<option />").val(this.Id).text(this.Name));
            });
        });
}
This assumes that Id and Name are properties of your object. You could use ID and Name depending on which drop down you're loading.
 Hope this helps,


Don't want to miss updates? Please click the below button!

Follow MindStick