ananth ganapathy

ananth ganapathy

  • NA
  • 72
  • 24.2k

Add New Field to Model Class Dynamically in MVC4

Feb 17 2014 5:46 AM

Hi

in have one table in my db, Table Name is Students

Column Name DataType

ID int

FieldName varchar(50)

and i insert some records in that table

below :

ID FieldName

1 ID

2 FirstName

3 LastName

4 Standard

5 Section

6 School

7 City

I insert 7 records

My Model Class

public class Studentdetails
{
public int UID { set; get; }
[Required]
public string ID { set; get; }
[Required]
public string FirstName { set; get; }
[Required]

public string LastName { set; get; }
[Required]
public string Standard { set; get; }
[Required]
public string Section { set; get; }
[Required]
public string School { set; get; }
[Required]
public string City { set; get; }
}

My Requirement is When I add New New Record in Students table

that will added dynamically in to Studentdetails class as new property

Ex Suppose i add new record like phno then my table come like this

ID FieldName

1 ID

2 FirstName

3 LastName

4 Standard

5 Section

6 School

7 City

8 Phno

i want my model automatically change in to below like this

public class Studentdetails
{
public int UID { set; get; }
[Required]
public string ID { set; get; }
[Required]
public string FirstName { set; get; }
[Required]

public string LastName { set; get; }
[Required]
public string Standard { set; get; }
[Required]
public string Section { set; get; }
[Required]
public string School { set; get; }
[Required]
public string City { set; get; }

public string Phno { set; get; }

}

anybody know pls answer this.........


Answers (2)