How to get a list from mvc controller to view using jquery ajax

前端 未结 6 1914
孤街浪徒
孤街浪徒 2021-02-04 08:02

I need to get a list from mvc controller to view using jquery ajax. how can i do that. this is my code. Its alerting error message.

In Controller

 publ         


        
6条回答
  •  [愿得一人]
    2021-02-04 08:19

    1-Create One model To NameOf User

    public class User
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Family { get; set; }
        }
    

    2-Create one ActionResult To NameOf GetUsers

    public ActionResult GetUsers()
            {
                List users = new List()
                {
                    new Models.User(){Id=1,Name="Diako",Family="Hasani"},
                    new Models.User(){Id=2,Name="Sina",Family="Moradi"},
                    new Models.User(){Id=3,Name="Hamid",Family="Chopani"}
                };
    
                return Json(users,JsonRequestBehavior.AllowGet);
            }
    

    3-To Your View Create One div

    4-write this code to script

    
    

提交回复
热议问题