c# - Create more than one object of the same type in the same view -


in create view want give user possibility create list of objects (of same type). therefore created table in view including each inputfield in each row. number of rows respective "creatable" objects fixed number.

lets there class book including 2 properties title , author , user should able 2 create 10 or less books.

how can that?

i don't know how pass list of objects (that binded) controller. tried:

[httppost] [validateantiforgerytoken] public actionresult create(icollection<book> booklist)     {         if (modelstate.isvalid)         {             foreach(var item in booklist)                 db.books.add(item);             db.savechanges();             return redirecttoaction("index");         }         return view(articlediscounts);     } 

and in view is:

<fieldset>     <legend>book</legend>      <table id="tablebooks" class="display" cellspacing="0" width="100%">             <thead>                 <tr>                     <th>title</th>                     <th>author</th>                 </tr>             </thead>             <tbody>                 @for (int = 0; < 10 ;i++ )                 {                     <tr>                         <td>                             <div class="editor-field">                                 @html.editorfor(model => model.title)                                 @html.validationmessagefor(model => model.title)                             </div>                         </td>                         <td>                             <div class="editor-field">                                 @html.editorfor(model => model.author)                                 @html.validationmessagefor(model => model.author)                             </div>                         </td>                     </tr>                 }             </tbody>         </table>      <p>         <input type="submit" value="create" />     </p> </fieldset> 

as booklist null, doesn't work , don't know how put created objects in list.

if have suggestions thankful.

the fact use @html.editorfor(model => model.title) suggests have declared model in view

@model yourassembly.book 

which allows to post 1 book post method need

public actionresult create(book model) 

note current implementation create inputs like

<input id="title" name="title" ... /> 

the name attributes not have indexers (they need name="[0].title", name="[1].title" etc.) cannot bind collection, , invalid html because of duplicate id attributes.

if want create 10 books, need initialize collection in method , pass collection view

public actionresult create() {   list<book> model = new list<book>();   for(int = 0; < 10;i++)   {     model.add(new book());   }   return view(model); } 

and in view

@model yourassembly.book @using (html.beginform()) {   for(int = 0; < model.count; i++)   {     @html.textboxfor(m => m[i].title)     @html.validationmessagefor(m => m[i].title)     .... // ditto other properties of book   }   <input type="submit" .. /> } 

which bind collection when post to

public actionresult create(list<book> booklist) 

note collection should list<book> in case need return view.

however may force user create 10 books, otherwise validation may fail (as suggested use of @html.validationmessagefor()). better approach dynamically add new book items in view using either begincollectionitem helper method (refer example) or client template per this answer.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -