How to pass List from Controller to View in MVC 3

后端 未结 4 955
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 15:17

I have a List<> binded with some data in Controller action and I want to pass that List<> to View to bind with DataGrid in Razor View.

I am new to MVC.Can any

4条回答
  •  难免孤独
    2020-12-14 15:46

    1. Create a model which contains your list and other things you need for the view.

      For example:

      public class MyModel
      {
          public List _MyList { get; set; }
      }
      
    2. From the action method put your desired list to the Model, _MyList property, like:

      public ActionResult ArticleList(MyModel model)
      {
          model._MyList = new List{"item1","item2","item3"};
          return PartialView(@"~/Views/Home/MyView.cshtml", model);
      }
      
    3. In your view access the model as follows

      @model MyModel
      foreach (var item in Model)
      {
         
      @item
      }

    I think it will help for start.

提交回复
热议问题