Hello fellow StackOverFlow, I need to make a INSERT
Multiple of records to my table through a dynamic form that I have created with JS.
My model is the following:
namespace MultiInsert.Models
{
public class Ejemplo
{
[Key]
public int id { get; set; }
public string objetivos { get; set; }
public string nombres { get; set; }
public decimal puntaje { get; set; }
}
}
My view is the following:
@model MultiInsert.Models.Ejemplo
@{
ViewBag.Title = "MultiCreate";
}
<h2>MultiCreate</h2>
@using (Html.BeginForm())
{
<div class="col-xs-12">
<div class="col-md-12">
<div id="field">
<div id="field0">
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="action_id">Objetivos</label>
<div class="col-md-5">
<input id="objetivos" name="objetivos" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<br><br>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="action_id">Nombres</label>
<div class="col-md-5">
<input id="nombres" name="nombres" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<br><br>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="action_name">Puntaje</label>
<div class="col-md-5">
<input id="puntaje" name="puntaje" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<br><br>
</div>
</div>
<!-- Button -->
<div class="form-group">
<div class="col-md-4">
<button id="add-more" name="add-more" class="btn btn-primary">Add More</button>
</div>
</div>
<br><br>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
My Script with which I insert inputs
dynamically:
<script>
$(document).ready(function () {
var next = 0;
$("#add-more").click(function (e) {
e.preventDefault();
var addto = "#field" + next;
var addRemove = "#field" + (next);
next = next + 1;
var newIn = ' <div id="field' + next + '" name="field' + next + '"><!-- Text input--><div class="form-group"><label class="col-md-4 control-label" for="action_id">Objetivos</label><div class="col-md-5"><input id="objetivos" name="objetivos" type="text" placeholder="" class="form-control input-md"></div></div><br><br><!-- Text input--><div class="form-group"><label class="col-md-4 control-label" for="action_id">Nombres</label><div class="col-md-5"><input id="nombres" name="nombres" type="text" placeholder="" class="form-control input-md"></div></div><br><br><!-- Text input--><div class="form-group"><label class="col-md-4 control-label" for="action_name">Puntaje</label><div class="col-md-5"><input id="puntaje" name="puntaje" type="text" placeholder="" class="form-control input-md"></div></div><br><br></div></div></div>';
var newInput = $(newIn);
var removeBtn = '<button id="remove' + (next - 1) + '" class="btn btn-danger remove-me" >Remove</button></div></div><div id="field">';
var removeButton = $(removeBtn);
$(addto).after(newInput);
$(addRemove).after(removeButton);
$("#field" + next).attr('data-source', $(addto).attr('data-source'));
$("#count").val(next);
$('.remove-me').click(function (e) {
e.preventDefault();
var fieldNum = this.id.charAt(this.id.length - 1);
var fieldID = "#field" + fieldNum;
$(this).remove();
$(fieldID).remove();
});
});
});
</script>
And finally part of my controller:
// View MultiCreate
public ActionResult MultiCreate()
{
return View();
}
// View MultiCreate
[HttpPost]
public ActionResult MultiCreate([Bind(Include = "id,objetivos,nombres,puntaje")] Ejemplo ejemplo)
{
if (ModelState.IsValid)
{
db.Ejemploes.Add(ejemplo);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(ejemplo);
}
I know that it is with a cycle
Foreach
that I have to carryINSERT
out, however I do not have the knowledge to carry out the process in MVC5.
I remain attentive to any news or doubt.
the solution is the next
In the view you have to set a loop:
In the controller
Create
first a list is set, and in the one that receives the form the cycle that is needed is thisAs for the
Script
is doing what is needed