Dynamically populate the drop-down using jQuery in ASP.Net MVC3

后端 未结 2 990
抹茶落季
抹茶落季 2020-12-04 01:05

I have two models:

public class ProfessorModels
{
    public string FullName { get; set; }
    public int ID { get; set; }
}

and

         


        
2条回答
  •  天命终不由人
    2020-12-04 01:23

    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:

    
    

    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($("

    This assumes that Id and Name are properties of your object. You could use ID and FullName depending on which drop down you're loading. I also use T4MVC to get the different method names.

    Hope this helps,

提交回复
热议问题