ASP.NET MVC: How do I pass a list (from a class in Model) to a repeater in a View?

前端 未结 4 2151
太阳男子
太阳男子 2020-12-30 15:25

How do I do the above? I\'ve started using MVC and I\'m having issues passing data around.

My specific problem is to do with a list of objects I have in my Model whi

4条回答
  •  萌比男神i
    2020-12-30 16:17

    The quick and dirty way is to pass it via ViewData

    public ActionResult List()
    {
        ViewData["MyList"] = new List () {"test1", "test2"};
    
        return View ();
    }
    

    then you can access it in your view

      <% foreach (string item in (List)ViewData["MyList"]) { %>
    • <%= item %>
    • <% }%>

提交回复
热议问题