I have the following form in my view
@using (Html.BeginForm("VMInstallCreate", "Scripts", FormMethod.Post, new { role = "form", encType = "multipart/form-data", id = "form_submit" }))
{
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<strong>Client</strong>
@Html.DropDownListFor(a => a.SelectedClientId, Model.Clients, "Select Client", new { @class = "form-control m-b" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>Host Instance IP</strong>
@Html.EditorFor(model => model.IP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.IP)
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>Host Instance User</strong>
@Html.EditorFor(model => model.User, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>
Host Instance Password
</strong>
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
</div>
<div id="vmDetails">
<div class="row" id="1">
<div class="col-md-3">
<div class="form-group">
<strong>VM Name</strong>
<input type="text" class="form-control" placeholder="" id="vmName1">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<strong>Disk 1 Size</strong>
<input class="touchspin1" type="text" value="" name="vmDisk1" id="vmDisk1">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<strong>RAM</strong>
<input class="touchspin1" type="text" value="" name="vmRAM1" id="vmRAM1">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<strong>Number of CPU</strong>
<input class="touchspin1" type="text" value="" name="vmCPU1" id="vmCPU1">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<a href="#" class="btn btn-primary" id="AddVM"><i class="fa fa-plus"></i> Add Another VM</a>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<button class="btn btn-primary" type="submit"><i class="fa fa-floppy-o"></i> Create</button>
<button>
</div>
</div>
</div>
}
And this page has the below java script
<script type="text/javascript">
$(document).ready(function () {
$("#AddVM").click(function () {
var count = $("#vmDetails").children().last().attr("id");
count++;
var html =
'<div class="row" id="' + count + '">' +
'<div class="col-md-3">' +
'<div class="form-group">' +
'<strong>VM Name</strong>' +
'<input type="text" class="form-control" placeholder="" id="vmName' + count + '">' +
'</div>' +
'</div>' +
'<div class="col-md-3">' +
'<div class="form-group">' +
'<strong>Disk 1 Size</strong>' +
'<input class="touchspin1" type="text" value="" name="vmDisk' + count + '" id="vmDisk' + count + '">' +
'</div>' +
'</div>' +
'<div class="col-md-3">' +
'<div class="form-group">' +
'<strong>RAM</strong>' +
'<input class="touchspin1" type="text" value="" name="vmRAM' + count + '" id="vmRAM' + count + '">' +
'</div>' +
'</div>' +
'<div class="col-md-3">' +
'<div class="form-group">' +
'<strong>Number of CPU</strong>' +
'<input class="touchspin1" type="text" value="" name="vmCPU' + count + '" id="vmCPU' + count + '">' +
'</div>' +
'</div>' +
'</div>';
$('#vmDetails').append(html);
});
$("#form_submit").submit(function (e) {
var form = $(this);
var url = form.attr('action');
$.ajax({
type: "POST",
url: url,
data: form.serialize(), // serializes the form's elements.
success: function (data) {
if (data != null) {
window.open(data, '_blank');
}
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
});
</script>
The user is able to add additional vm's to the div, this part works fine.
I have the below model for this page
public class VMInstall
{
public int SelectedClientId { get; set; }
public IEnumerable<SelectListItem> Clients { get; set; }
public string Initials { get; set; }
public string IP { get; set; }
public string User { get; set; }
public string Password { get; set; }
//[DataType(DataType.MultilineText)]
//public string VMDetails { get; set; }
public List<VMDetails> VMDetails { get; set; }
public VMInstall()
{
Clients = Common.GetClients();
IP = "10.27.50.200";
User = "root";
Password = "92dT8C9Dnk";
}
}
public class VMDetails
{
public string VMname { get; set; }
public int disk1size { get; set; }
public int memorysize { get; set; }
public int numCPU { get; set; }
public VMDetails()
{
disk1size = 60;
memorysize = 8;
numCPU = 2;
}
}
How do i using Jquery, add the inputs of the vmdetails to the form before its submitted? Eg i need to add to the List of VMDetails in VMInstall from the input fields?