c# - How to Insert Multiple rows having dropdownlist in asp.net MVC? -


i searched lot , tried few things did not work out. need resolve problem. wanted instert multiple records in child1 table have foreign key(i.e. dropdownlist).

i have table child1with following columns

public int id_child1 { get; set; }  public int parentid{ get; set; } public string data1{ get; set; } public datetime col1{ get; set; } 

parentid foreign key referring parent table.

after following 1 of method youtube, wrote code below- controller-

 public class child1controller : controller     {         private testentities db = new testentities();          public actionresult bulkdata()         {             viewbag.parentid = new selectlist(db.parents, "parentid", "data");             // show default 1 row insert data database             list<child1> ci = new list<child1> { new child1 {  parentid = 0, data = "" ,col1=datetime.now} };             return view(ci);         }          [httppost]         [validateantiforgerytoken]         public actionresult bulkdata( list<child1> ci)         {             if (modelstate.isvalid)             {                 using (testentities dc = new testentities())                 {                     foreach (var in ci)                     {                         dc.child1.add(i);                     }                     dc.savechanges();                     viewbag.message = "data saved!";                     modelstate.clear();                     ci = new list<child1> { new child1 {  parentid =  0, data = "", col1 = datetime.now } };                 }             }             viewbag.parentid = new selectlist(db.parents, "parentid", "data");             return view(ci);         } } 

view-

   @model list<testing.models.child1>     @{         viewbag.title = "insert bulk data";     }          @using (html.beginform("bulkdata", "child1", formmethod.post))         {             @html.antiforgerytoken()             @html.validationsummary(true)              if (viewbag.message != null)             {                 <div style="border:solid 1px green">                     @viewbag.message                 </div>             }              <div><a href="#" id="addnew">add new</a></div>             <table id="datatable" border="0" cellpadding="0" cellspacing="0">                 <tr>                     <th>parentid</th>                     <th>data</th>                     <th>date</th>                     <th></th>                 </tr>                 @if (model != null && model.count > 0)                 {                     int j = 0;                     foreach (var in model)                     {                         <tr style="border:1px solid black">                             <td>@html.dropdownlist("parentid","parentid")</td>                             <td>@html.textboxfor(a => a[j].data)</td>                             <td>@html.editorfor(a=> a[j].col1)</td>                             <td>                                 @if (j > 0)                                 {                                     <a href="#" class="remove">remove</a>                                 }                             </td>                         </tr>                         j++;                     }                 }             </table>             <input type="submit" value="save bulk data" />         } //jquery code dynamically add rows , remove rows 

when add multiple rows , save record, first row saved in db parentid null. wanted save records proper parent id. enter image description here


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -