I have a form in ASP.Net MVC where contacts can be one or many
for example: a project can have one or many contact persons. I created my form it it has a div with two textboxes like blow:
<div class="form-group">
<label class="control-label col-md-2">Focal Points</label>
<div class="col-md-10">
<div class="input_fields_wrap">
<button class="add_field_button btn">Add More Focal Points</button>
<div style="margin:4px;">
Name: <input type="text" name="contact_name[]" />
Phone <input type="text" name="contact_phone[]" />
</div>
</div>
</div>
</div>
when I post my form how can I get the array of contact names and phones as a single array of key and values.
script that generates textboxes dynamically:
<script>
$(document).ready(function () {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function (e) { //on add input button click
e.preventDefault();
if (x < max_fields) { //max input box allowed
x++; //text box increment
$(wrapper).append('<div style="margin:4px;">Name: <input type="text" name="contact_name[]"/> Phone <input type="text" name="contact_phone[]" /><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});