c# - MVC Updating related data -


this question has answer here:

i have many-to-many relationship 3 tables. employee, history , title. history-table contains timespan , relationship both employee , title.

in end want able edit both timespan , title each employee common view. in employeecontroller.cs i've got code when submit form.

[httppost, actionname("edit")] [validateantiforgerytoken] public actionresult edit(employee empl) // note: have no bindings here, either. should i?     if (modelstate.isvalid)     {         db.entry(empl).state = entitystate.modified;         db.savechanges();         return redirecttoaction("index");     }      return view(empl); } 

but object empl doesn't have in it's histories-collection. says count = 0. how can include both history (and later title-table) can update that, too?

some of employee.cs:

public partial class employee {     public employee()     {         this.histories = new hashset<histories>();     }     public virtual icollection<histories> histories { get; set; } } 

for reference, shows correctly in edit.cshtml employee:

<table class="table"> <tr>     <th>start date</th>     <th>end date</th>     <th>title</th> </tr> @foreach (var item in model.histories.orderbydescending(x => x.fromdate)) {     <tr>         <td>             @html.editorfor(modelitem => item.fromdate)         </td>         <td>             @html.editorfor(modelitem => item.todate)         </td>         <td>             @html.editorfor(modelitem => item.title.title)         </td>     </tr> } </table> 

if more info needed, feel free ask, , sorry typos. had translate class names english you'll understand better.

use seomthing this:

            (int = 0; <  model.histories.orderbydescending(x => x.fromdate).count(); i++)             {                 <tr>                     <td>                         @html.hiddenfor(modelitem => model.histories[i].fromdate)                                                     @html.editorfor(modelitem => model.histories[i].fromdate)                                                 </td>                     <td>                         @html.hiddenfor(modelitem => model.histories[i].todate)                                                     @html.editorfor(modelitem => model.histories[i].todate)                        </td>                     <td style="text-align: center">                         @html.hiddenfor(modelitem => model.histories[i].title.title)                                                     @html.editorfor(modelitem => model.histories[i].title.title)                                                  </td>                 </tr>             } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -