entity framework - ASP.NET MVC 4 Error Saving ViewModels -


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 studentviewmodel()     { }      } 

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,                  dateofreg = 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,                 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");     } 

view is:

@using (html.beginform()) { @html.validationsummary(true)  <fieldset>     <legend>doctor</legend>       @html.editorfor(model => model.student.name )     @html.editorfor(model => model.student.speciality)     @html.editorfor(model => model.student.dateofjoinig)     @html.editorfor(model => model.student.standard)      @html.hiddenfor(model => model.student.studentid)     @html.editorfor(model => model.studentaddress.address)     @html.editorfor(model => model.studentaddress.area)     @html.editorfor(model => model.studentaddress.city)     @html.editorfor(model => model.studentaddress.state)      @html.hiddenfor(model => model.student.studentid)     @html.editorfor(model => model.studentphoto.photo)       <p>                 <input type="submit" value="create" />             </p>         </fieldset>     }      <div>         @html.actionlink("back list", "index")     </div> 

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.

my problem when try save data redirected create page. "modelstate.isvalid" false no data saved. please me how proceed.

thanks.

this line @ top of action wrong:

if (modelstate.isvalid)     {     return view(studentviewmodel);     } 

it should opposite, if model not valid, should stop process , re-render view form.

try:

if (!modelstate.isvalid)     {     return view(studentviewmodel);     } 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -