DYNAMICALLY POPULATE THE DROP-DOWN USING JQUERY IN ASP.NET MVC3

sandy taylor

Total Post:46

Points:324
Posted by  sandy taylor
.NET  JQuery  Mvc3 
 889  View(s)
Ratings:
Rate this:
I have a model
public class City
{
    public string Name { get; set; }
    public int ID { get; set; }
}

  1. Anurag Sharma

    Post:40

    Points:280
    Re: Dynamically populate the drop-down using jQuery in ASP.Net MVC3

    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,

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!