0

when i am running my code then i am getting this error

Models.States does not contain a property with the name 'ID'

but my state and city class has property called ID and name

here i am giving my full code. just see and tell me where i am making the mistake?

viewmode and model class

public class MainViewModel
{
    public List<Student> Students { get; set; }
    public int SelectedState = 0;
    public int SelectedCity = 0;
}

public class Student
{
    public int ID = 0;
    public string Name = "";
    public int StateID = 0;
    public int CityID = 0;
    public List<States> States { get; set; }
    public List<Cities> Cities { get; set; }

}

public class States
{
    public int ID = 0;
    public string Name = "";
}

public class Cities
{
    public int ID = 0;
    public string Name = "";
}

Controller from where i am manually populate my model

 public ActionResult Index()
        {
            MainViewModel oVm = new MainViewModel()
            {
                Students = new List<Student>() {
                    new Student
                    {
                        ID=1,
                        Name="JoyDev",
                        StateID=1,
                        CityID=1,
                        States=new List<States>()
                        {
                            new States
                            {
                                ID=1,
                                Name="WestBengal",
                            },
                            new States
                            {
                                ID=2,
                                Name="Bihar",
                            },
                            new States
                            {
                                ID=3,
                                Name="Orrisa",
                            }

                        },
                        Cities=new List<Cities>()
                        {
                            new Cities
                            {
                                ID=1,
                                Name="Alipur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Asansol"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Andul"
                            }

                        }
                    },

//***********
                    new Student
                    {
                        ID=1,
                        Name="Mukti",
                        StateID=2,
                        CityID=1,
                        States=new List<States>()
                        {
                            new States
                            {
                                ID=1,
                                Name="WestBengal",
                            },
                            new States
                            {
                                ID=2,
                                Name="Bihar",
                            },
                            new States
                            {
                                ID=3,
                                Name="Orrisa",
                            }

                        },
                        Cities=new List<Cities>()
                        {
                            new Cities
                            {
                                ID=1,
                                Name="Janpur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Madhubani"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Kanti"
                            }

                        }
                    },
//***********
                    new Student
                    {
                        ID=1,
                        Name="Somnath",
                        StateID=3,
                        CityID=2,
                        States=new List<States>()
                        {
                            new States
                            {
                                ID=1,
                                Name="WestBengal",
                            },
                            new States
                            {
                                ID=2,
                                Name="Bihar",
                            },
                            new States
                            {
                                ID=3,
                                Name="Orrisa",
                            }

                        },
                        Cities=new List<Cities>()
                        {
                            new Cities
                            {
                                ID=1,
                                Name="Chandapur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Dhankauda"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Konarak"
                            }

                        }
                    }


                }

            };

            return View(oVm);
        }

My view code

@model BuildTable.Models.MainViewModel
@{
    ViewBag.Title = "Home Page";
}

<div>
    <table>
    <tr>
        <td>ID</td>
        <td>Name</td>
        <td>State</td>
        <td>City</td>
    </tr>

        @for(int i = 0; i < Model.Students.Count; i++)
        {
            <tr>
                <td><input type="text" value="@Model.Students[i].ID" /></td>
                <td><input type="text" value="@Model.Students[i].Name" /></td>
                <td>
                    @Html.DropDownListFor(m => m.Students[i].StateID, new SelectList(Model.Students[i].States, "ID", "Name",Model.Students[i].StateID), "-- Select States--", new { @class = "edit-mode" })
                </td>
                <td>
                    @Html.DropDownListFor(m => m.Students[i].CityID, new SelectList(Model.Students[i].Cities, "ID", "Name",Model.Students[i].CityID), "-- Select Cities--", new { @class = "edit-mode" })
                </td>
            </tr>
        }
    </table>
</div>

please tell me what is mistake in code. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • 5
    Probably you need property `public int ID { get; set; }` not field. – Gene R Jan 11 '16 at 13:18
  • you have getter and setter in your models for Id, Name, etc. – Kami Jan 11 '16 at 13:20
  • not clear. i have id property in state and city class – Mou Jan 11 '16 at 13:20
  • @Mou It's not properties. You have fields. [Property vs fields](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c). If you using MVC and View models you should define only properties becouse binding works only with them – teo van kot Jan 11 '16 at 13:22
  • would u plzz come with code where i made the mistake? – Mou Jan 11 '16 at 13:22
  • yes i understood what mistake i made. thanks to all – Mou Jan 11 '16 at 13:24

1 Answers1

3

update your States and Cities classes as follows

public class States
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class Cities
{
    public int ID { get; set; }
    public string Name { get; set; }
}
Zergling
  • 526
  • 2
  • 7