entity framework - Save/Update Data into multiple tables using ASP.NET MVC 4 -


can me on how save , update data multiple entities using viewmodel?

i have viewmodel looks this:

  public class studentviewmodel   {     public student student;     public studentaddress studentaddress { get; set; }     public studentphoto studentphoto { get; set; }     // 3 entities related 1 one relationship      public doctordetailsviewmodel()     { }      } 

my controller is:

  [httppost]     public actionresult create(studentviewmodel studentviewmodel)     {         if (modelstate.isvalid)         {             return view(studentviewmodel);         }              student s = new student()              {                  name =studentviewmodel.student.name,                  speciality = studentviewmodel.student.speciality,                  dateofjoinig = studentviewmodel.student.dateofjoinig,                  qualification = studentviewmodel.student.qualification,                  email = studentviewmodel.student.email               };              studentaddress sa = new studentaddress()             {                 studentid= studentviewmodel.student.studentid,                 address = studentviewmodel.studentaddress.address,                 area = studentviewmodell.studentaddress.area,                 city = studentviewmodel.studentaddress.city,                 state = studentviewmodel.studentaddress.state,                 pincode = studentviewmodel.studentaddress.pincode,                 phone = studentviewmodel.studentaddress.phone,                 mobile = studentviewmodel.studentaddress.mobile              };              studentphoto sp = new studentphoto()             {                 studentid= studentviewmodel.student.studentid,                 photo = studentviewmodel.studentphoto.photo              };                 db.students.add(s);             db.studentaddress.add(sa);             db.studentphoto.add(sp);              db.savechanges();             return redirecttoaction("home");     } 

i able retrieve , display data (from multiple entities) view. however, i'm stuck on how can save , update above entities new data. of examples 1-1 relationship mapping automatic, in case data belongs multiple entities.

what best way this? thanks.

first of models should this:

public class student {     [key]     [databasegenerated(databasegeneratedoption.identity)]     public int studentid { get; set; }      public string speciality { get; set; }      public datetime dateofjoinig { get; set; }      public string qualification { get; set; }      public string email { get; set; } }  public class studentaddress {     [key]     [databasegenerated(databasegeneratedoption.identity)]     public int studentaddressid { get; set; }      [required]     [foreignkey("student")]     public int studentid { get; set; }      public virtual student student { get; set; }      public string address { get; set; }      public string area { get; set; }      public string city { get; set; }      public string state { get; set; }      public string pincode { get; set; }      public string phone { get; set; }      public string mobile { get; set; } }  public class studentphoto {     [key]     [databasegenerated(databasegeneratedoption.identity)]     public int studentphotoid { get; set; }      [required]     [foreignkey("student")]     public int studentid { get; set; }      public virtual student student { get; set; }      public string photo { get; set; } } 

as qazi mentioned should have navigation properties. on client side should have hidden fields id properties editing purpose:

@html.hiddenfor(s => s.studentid) 

after controller method this:

    [httppost]     public actionresult create(studentviewmodel studentviewmodel)     {         if (!modelstate.isvalid)         {             return view(studentviewmodel);         }          studentviewmodel.studentaddress.student = studentviewmodel.student;         studentviewmodel.studentphoto.student = studentviewmodel.student;         if (studentviewmodel.student.studentid > 0)             db.students.attach(studentviewmodel.student);         else             db.students.add(studentviewmodel.student);         db.savechanges();         return redirecttoaction("home");     } 

you don't need assign id properties, need assign navigation properties.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -