Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Basically in jQuery, I clone a div multiple time and inside it I have 3 textboxes and a dropdown. Now, I want to get 3 text box and my one dropdown value inside every div in array and want to pass that in controller.
Ex:


<div class="col-md-12 m-t-5 NewVehicle">
                                            
                                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                                                <div class="form-group">
                                                    <label for="VehicleYear">Year/Make</label><span class="text-danger">*</span>
                                                    <div style="display:flex;">
                                                        <input class="form-control m-r-5" id="txtVehicleYear" type="text" />
                                                        <input class="form-control" id="txtVehicleMake" type="text">
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label for="VehicleModel">Model</label><span class="text-danger">*</span>
                                                    <input class="form-control" id="txtVehicleModel" type="text">
                                                </div>
                                                <div class="form-group">
                                                    <label for="VehicleType">Vehicle Type</label><span class="text-danger">*</span>
                                                    @Html.DropDownListFor(Model => Model.lstVehicleType, new SelectList(Model.lstVehicleType, "Value", "Text"), new { @class = "form-control", @id = "ddlNewVehicleType" })
                                                </div>
                                            </div>
                                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                                            </div>
                                        </div>


What I have tried:

I have tried with jquery but all values in inside comes in a single array.
var inputArray = [];
$('#dvNewVehicle').find("select, text, input").each(function () {
var cntrlId = $(this).attr('id');
inputArray.push($("#" + cntrlId + "").val().trim());
});

above returns me value1, value2, value3, value4, value5, value6 but I want it as [{value1, value2, value3},{value4, value5, value6}].
one div values in one list.
Posted
Updated 20-Jul-17 0:06am

1 solution

A few issues, first of all http and forms worked fine long before js was invented, you don't need js to do basic http like submit form data to an action. Next you are duplicating ids and ids need to be unique; duplicating them can lead to unexpected behaviours. Also ids are only used for js, css etc, they are not submitted with a form, you need a "name" for that.

Simplest solution to your problem is to change your ids to names and amend the target action

<div class="col-md-12 m-t-5 NewVehicle">
                                            
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
        <div class="form-group">
            <label for="VehicleYear">Year/Make</label><span class="text-danger">*</span>
            <div style="display:flex;">
                <input class="form-control m-r-5" name="txtVehicleYear" type="text" />
                <input class="form-control" name="txtVehicleMake" type="text">
            </div>
        </div>
        <div class="form-group">
            <label for="VehicleModel">Model</label><span class="text-danger">*</span>
            <input class="form-control" name="txtVehicleModel" type="text">
        </div>
        <div class="form-group">
            <label for="VehicleType">Vehicle Type</label><span class="text-danger">*</span>
            <select name="ddlNewVehicleType">
                <option value="1">One</option>
                <option value="2">Two</option>
                <option value="3">Three</option>
            </select>
        </div>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
    </div>
</div>


[HttpPost]
public ActionResult Index(string[] txtVehicleYear, string[] txtVehicleMake, string[] txtVehicleModel, string[] ddlNewVehicleType)
{
    // you'll probably want some validation that all arrays are the same length

    for (int i = 0; i < txtVehicleYear.Length; i++)
    {
        string year = txtVehicleYear[i];
        string make = txtVehicleMake[i];
        string model = txtVehicleModel[i];
        string type = ddlNewVehicleType[i];
    }

    return View();
}


Update

Below are two divs (I've hard-coded them for simplicity) inside a form that is then submitted via ajax and serialisation

<form id="myForm">
    <div class="col-md-12 m-t-5 NewVehicle">

        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <div class="form-group">
                <label for="VehicleYear">Year/Make</label><span class="text-danger">*</span>
                <div style="display:flex;">
                    <input class="form-control m-r-5" name="txtVehicleYear" type="text" />
                    <input class="form-control" name="txtVehicleMake" type="text">
                </div>
            </div>
            <div class="form-group">
                <label for="VehicleModel">Model</label><span class="text-danger">*</span>
                <input class="form-control" name="txtVehicleModel" type="text">
            </div>
            <div class="form-group">
                <label for="VehicleType">Vehicle Type</label><span class="text-danger">*</span>
                <select name="ddlNewVehicleType">
                    <option value="1">One</option>
                    <option value="2">Two</option>
                    <option value="3">Three</option>
                </select>
            </div>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
        </div>
    </div>

    <div class="col-md-12 m-t-5 NewVehicle">

        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <div class="form-group">
                <label for="VehicleYear">Year/Make</label><span class="text-danger">*</span>
                <div style="display:flex;">
                    <input class="form-control m-r-5" name="txtVehicleYear" type="text" />
                    <input class="form-control" name="txtVehicleMake" type="text">
                </div>
            </div>
            <div class="form-group">
                <label for="VehicleModel">Model</label><span class="text-danger">*</span>
                <input class="form-control" name="txtVehicleModel" type="text">
            </div>
            <div class="form-group">
                <label for="VehicleType">Vehicle Type</label><span class="text-danger">*</span>
                <select name="ddlNewVehicleType">
                    <option value="1">One</option>
                    <option value="2">Two</option>
                    <option value="3">Three</option>
                </select>
            </div>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
        </div>
    </div>
</form>

<input value="Click" type="button" onclick="submit()"/>

<script>
    function submit() {
        $.ajax({
            method: 'POST',
            url: '@Url.Action("Index", "Home")',
            data: $("#myForm").serialize()
        });
    }

</script>


The controller method is the same as above
 
Share this answer
 
v2
Comments
kkakadiya 20-Jul-17 6:11am    
I am making AJAX call instead posting form.
F-ES Sitecore 20-Jul-17 6:13am    
Serialise the form values and use that as the data, google "jquery serialise form data", it has fairly simple methods for doing this.
kkakadiya 20-Jul-17 6:16am    
I have unique Ids as I am giving index to Id. ex: txtVehicleYear-1,txtVehicleYear-2,-3, etc. and this is for every textbox and dropdown.
New Vehicle button code

var id = 0;
$("#btnAddVehicle").click(function () {
$(".NewVehicle").clone().attr('id', 'dv-' + (id += 1)).addClass('addVechicle').insertAfter('.NewVehicle').removeClass("NewVehicle").appendTo("#dvNewVehicle");
$(".addVechicle").css("display", "inline");
$(".addVechicle").find('#btnClearVehicle').attr('id', 'btnClearVehicle-' + id).attr('onclick', 'RemoveAddVehicle(' + id + ')');
$(".addVechicle").find('#txtVehicleYear').attr('id', 'txtVehicleYear-' + id);
$(".addVechicle").find('#txtVehicleMake').attr('id', 'txtVehicleMake-' + id);
$(".addVechicle").find('#txtVehicleModel').attr('id', 'txtVehicleModel-' + id);
$(".addVechicle").find('#ddlNewVehicleType').attr('id', 'ddlNewVehicleType-' + id).attr('name', 'ddlNewVehicleType-' + id);
});
F-ES Sitecore 20-Jul-17 6:22am    
You can keep the ids, it's not a problem, just make sure they have the same name.

Also do you see already how much of this info was not in your original question? To get more accurate answers you have to give all of the relevant info up front.
kkakadiya 20-Jul-17 6:24am    
sorry for that, I posted what is the core of question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900