Monday, April 20, 2015

mvc model has a list

http://stackoverflow.com/questions/25092497/c-sharp-mvc-list-and-create-in-same-view


http://www.codeproject.com/Tips/855577/List-of-Model-Object-Post-to-Controller-in-ASP-NET

hat is so mach pretty easier 

        @using (Html.BeginForm())
        {
            @Html.DisplayFor(model => model.Step) 
 
 
            for (var i = 0 ; i< Model.List.Count; i++)
            {
                <span>@Html.TextBoxFor(m => m.List[i].ID) </span> <span>@Html.TextBoxFor(m => m.List[i].Name) </span> 
 

            
            }
            
            <input type="submit" value="Submit" />
        }

and controller

        public ActionResult Index()
        {
            var model = new MyModel(){Step = 1, List = new List<MyModelEnum>()};
 
            model.List.Add(new MyModelEnum() { ID = 1, Name = "1"});
            model.List.Add(new MyModelEnum() { ID = 2, Name = "2" });
            model.List.Add(new MyModelEnum() { ID = 3, Name = "3" });
            model.List.Add(new MyModelEnum() { ID = 4, Name = "4" });
            model.List.Add(new MyModelEnum() { ID = 5, Name = "5" });
            return View(model);
        }
 
        [HttpPost]
        public ActionResult Index(MyModel model)
        {
            return View(model);
        }

and model

  public class MyModel 
    {
        public int Step { get; set; }
 
        public List<MyModelEnum> List { get; set; }
    }
 
    public class MyModelEnum
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

No comments:

Post a Comment