c# - Insert Multiple rows in a table using MVC Razor Views -


i have scenario in need store multiple rows in single table.let me explain in detail.

i have table price has 4 columns, id, modelid, appsettingid,amount.

i looking inserting multiple values table

  1. id primary key.
  2. modelid same rows.
  3. appsettingid , amount different rows , based on selection user on view.

i have bound appsettingid different combo boxes on view have categorized in database.

this doing right now.

view:

<div class="editor-label">         @html.labelfor(model => model.modelid, "model")     </div>     <div class="editor-field">         @html.dropdownlist("modelid", string.empty)         @html.validationmessagefor(model => model.modelid)     </div>      <div class="editor-label">         @html.labelfor(model => model.appsettingid, "mobile condition")     </div>      <div class="editor-field">         @html.dropdownlist("mobile condition", new       selectlist(viewbag.conditionid, "text", "value"))         @html.validationmessagefor(model => model.appsettingid)     </div>      <div class="editor-label">         @html.labelfor(model => model.amount)     </div>     <div class="editor-field">         @html.editorfor(model => model.amount)         @html.validationmessagefor(model => model.amount)     </div>      <div class="editor-label">         @html.labelfor(model => model.appsettingid, "tennure")     </div>      <div class="editor-field">         @html.dropdownlist("tennure", new selectlist(viewbag.appsettingid, "text", "value"))         @html.validationmessagefor(model => model.appsettingid)     </div>      <div class="editor-label">         @html.labelfor(model => model.amount)     </div>     <div class="editor-field">         @html.editorfor(model => model.amount)         @html.validationmessagefor(model => model.amount)     </div>      <p>         <input type="submit" value="create" />     </p> 

controller:

public actionresult create()     {         //viewbag.appsettingid = db.appsettings.select(r => r.id).distinct();         viewbag.modelid = new selectlist(db.models, "id", "name");         //viewbag.tennure = db.appsettings.select(s => s.type == "tennure").distinct();          iqueryable<appsetting>tennureids = db.appsettings.where(s => s.type == "tennure").distinct();         iqueryable<appsetting> conditions = db.appsettings.where(s => s.type == "mobile condition").distinct();          list<selectlistitem> items = new list<selectlistitem>();         foreach (var t in tennureids)         {             selectlistitem s = new selectlistitem();             s.text = t.id.tostring();             s.value = t.value.tostring();             items.add(s);         }         viewbag.appsettingid = items;          list<selectlistitem> conds = new list<selectlistitem>();         foreach (var t in conditions)         {             selectlistitem s = new selectlistitem();             s.text = t.id.tostring();             s.value = t.value.tostring();             conds.add(s);         }          viewbag.conditionid = conds;          return view();     }      //     // post: /price/create      [httppost]     [validateantiforgerytoken]     public actionresult create(price price, formcollection form)     {         if (modelstate.isvalid)         {             int test = convert.toint16(form["mobile condition"]);             price.appsettingid = test;             db.prices.add(price);             db.savechanges();             return redirecttoaction("index");         }          //viewbag.appsettingid = new selectlist(db.appsettings, "id", "type", price.appsettingid);         //viewbag.modelid = new selectlist(db.models, "id", "name", price.modelid);         return view(price);     } 

i hope solution

the following razor page, here have displayed model in table format

@model list<mymodel>  <h2>create</h2>  @using (html.beginform()) {     <table>         <tr>             <th>                 model id             </th>             <th>                 app setting id             </th>                       <th>                 amount             </th>                     </tr>          @for (int = 0; < model.count; i++)         {             <tr>                 <td>                     @html.textboxfor(x => model[i].modelid)                 </td>                 <td>                     @html.textboxfor(x => model[i].appsettingid)                 </td>                 <td>                     @html.textboxfor(x => model[i].amount)                 </td>             </tr>                             }     </table>     <input type="submit" /> } 

when user clicks submit button, pass model controller

controller

    [httppost]     public actionresult create(list<mymodel> m)     {         // coding         return view("create", m);     } 

Comments