You can use the following view, which will add a new entry using HTTP Post instead of Ajax. Replacing it with Ajax.BeginForm with the appropriate parameters, use Ajax instead of a regular mail request.
@using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <table class="list-chiller-record"> @for (int i = 0; i < this.Model.ChillerDetails.Count; i++) { if (i == 0) { <tr class="chiller-record-template" style="display:none"> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerAge)</td> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerBrand)</td> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerCapacity)</td> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerRefrigerant)</td> </tr> } <tr class="chiller-record"> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerAge)</td> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerBrand)</td> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerCapacity)</td> <td>@Html.EditorFor(x=>x.ChillerDetails[i].ChillerRefrigerant)</td> </tr> } </table> <br /> <input type="button" class="add-button" name="add" value="Add" /> <input type="submit" class="save-button" name="save" value="save" /> }
Add add new line:
<script type="text/javascript"> $(document).ready(function () { var count = 2; $('.add-button').click(function () { count++; var template = $('.chiller-record-template').clone() template.find('input[type=text]').val(''); $.each(template.find('input[type=text]'), function () { var name = $(this).attr('name'); name = name.replace('0', count - 1); $(this).attr('name', name); }); $('.list-chiller-record').append(template); template.removeClass('chiller-record-template').addClass('chiller-record').show(); }) }); </script>
Your action may be as follows:
[HttpPost] public ActionResult AddHealthCheck(AddHealthCheckFormModel model) { if (ModelState.IsValid) { HealthCheckRepository healthCheckRepository = new HealthCheckRepository(); healthCheckRepository.save(model); } return this.View(model); }
And in the repository, you can actually save the data in the database. You can use EF or any other ORM for this.
source share