How to add an arbitrary number of rows to a database in ASP.net MVC 4? -


i'm trying add unspecified number of rows database. these rows created dynamically user's request:

$(document).ready(function () {      $(document).on('click', '#datatable .add', function () {          var row = $(this).closest('tr');          if ($('#datatable .add').length <= 100) {             var clone = row.clone();              // clear values.             var tr = clone.closest('tr');             tr.find('input[type=text]').val('');             $(this).closest('tr').after(clone);         }      });      // delete row if there exists more one.     $(document).on('click', '#datatable .removerow', function () {         if ($('#datatable .add').length > 1) {             $(this).closest('tr').remove();         }     }); }); 

so far using try , achieve list maximum value of 100 elements (which i'd prefer different method this, perhaps 1 no upper limit in meantime), , pass list i've created view():

// get: questions/create public actionresult create(int questionnaireuid) {     list<question> questions = new list<question>();     (var = 0; < 100; i++)     {         questions.add(new question { questionnaireuid = questionnaireuid, question1 = "" });     }     return view(questions); } 

in view() here pertinent code sample using populate values of list (i believe problems lie here...):

<table id="datatable" name="datatable"> @if (model != null && model.count() > 0) {     <tr>         @html.hiddenfor(model => model[i].questionnaireuid)         <td>             @html.editorfor(model => model[i].question1, new { htmlattributes = new { @type = "text", @name = "question", @class = "question_input" } })             @html.validationmessagefor(model => model[i].question1, "", new { @class = "text-danger" })             <input type="button" name="addrow[]" class="add" value="add">             <input type="button" name="addrow[]" class="removerow" value="remove">         </td>     </tr>     i++; } </table> 

when try save created list database, using code below:

[httppost] [validateantiforgerytoken] public actionresult create([bind(include = "questionnaireuid, question1")] list<question> questions) {     if (modelstate.isvalid)     {         foreach (var question in questions)         {             db.questions.add(question);         }         db.savechanges();         return redirecttoaction("index");     }     return view(questions); } 

i able save first element of list. size of list one, regardless of if user dynamically generated 10 list elements.

furthermore, if first element has error (e.g. empty) error message printed elements in table. so, being said although list able generate in view multiple elements/rows, database 1 row meaningful/used--which first row. how can fix this?

as far know, mvc doesn't provide built-in solution handling dynamic/variable length lists. there few custom solutions involve creating helpers handle posting dynamic list objects. here 1 have used many times in past, , compatible both old , current versions of asp mvc (the blog uses old mvc markup, need update razor code mvc3+).

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

the key component of helper class "begincollectionitem", handles indexing of list in way mvc accept post-action parameter. hope helps.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -