mardi 10 novembre 2015

How to combine a list model to another model in ASP.NET MVC

I have two models:

public class Model1
    {
        public int? Id { get; set; }    
        public string Name{ get; set; }
        private IList<Model2> _model2; 
    } 

  public class Model2
        {
           public string Name2{ get; set; }
        }

JSON data for ajax call:

[
  {
    "Id": 1,
    "Name": "Foo",
    "Name2": "Foo2"
  },
  {
    "Id": 2,
    "Name": "Foo3",
    "Name2": "Foo4"
  }
]

Controller action:

public ActionResult Save(List<Model1> models)
        {
        }

In this way I do not get Name2 in controller action. Are there any way to get Name2 in controller?

I know it can be solved by creating another model. e.g

public class Model3
        {
            public int? Id { get; set; }    
            public string Name{ get; set; }
            private Model2 _model2; 
        } 

But I don't want to create new model class. thanks....

Aucun commentaire:

Enregistrer un commentaire