1

I have following razor code. I need to add this dynamically on button click in client side.

       <div class="form-group" id="transitem" name="transitem">
        <div class="col-md-11" id="tritem" name="tritem">
            @Html.LabelFor(model => model.transaction_item_id, "transaction_item_id", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div id="trans_dd_1" name="trans_dd_1">@Html.DropDownList("transaction_item_id", null, htmlAttributes: new { @class = "form-control", @id = "trans_id_#", @Name = "trans_id_#" })</div>
                @Html.ValidationMessageFor(model => model.transaction_item_id, "", new { @class = "text-danger" })
            </div>


            <div>
                @Html.LabelFor(model => model.transaction_itmQty, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-3">
                    @Html.EditorFor(model => model.transaction_itmQty, new { htmlAttributes = new { @class = "form-control", @id = "trans_qty_#", @Name = "trans_qty_#" } })
                    @Html.ValidationMessageFor(model => model.transaction_itmQty, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="col-md-2"><input type="button" class="btnPlus" value="+" /></div>
        </div>
    </div>

I have written following script, but it is not working as expected. I need to change the name and id of trans_id_# and trans_qty_#. They are being generated by html helper, html.editorfor. Below is my script, which can copy the new element, but i am not able to change the id or name.

<script type="text/javascript">
var itm_cnt = 1;
$(document).ready(function () {
    $(document).on("click", ".btnPlus", function () {

        var new_clone = $('#tritem').clone();
        itm_cnt = itm_cnt + 1;
        var new_name = "trans_id_" + itm_cnt.toString();
        $(new_clone).attr("name", "new_name");

        $('#transitem').append(new_clone);
        window.alert(itm_cnt);
    });
});

  • 5
    I have already given you 3 links in your [previous question](http://stackoverflow.com/questions/34993595/how-to-pass-data-from-view-to-controller-in-asp-net-mvc) showing you how to do this correctly –  Jan 27 '16 at 11:15
  • @StephenMuecke thats not working. – Byomkesh Bakhsy Jan 27 '16 at 11:17
  • Of course it works.If its not working for you, then you have an error in your code. –  Jan 27 '16 at 11:18
  • @ByomkeshBakhsy as Stephen has suggested in your previous question you have to use BeginCollectionItem - with that library you will not have duplicate Id and name attributes: http://stackoverflow.com/a/28081308/1910735 – Dawood Awan Jan 27 '16 at 11:23
  • by not working i mean, the names are not changing properly. The first one stays with fake indexer #. How to change that one too? – Byomkesh Bakhsy Jan 27 '16 at 11:34
  • @DawoodAwan i believe i can get desired result by second approach with fake indexer. the only trouble is the first one stays with name and index ending with #. – Byomkesh Bakhsy Jan 27 '16 at 11:36
  • @ByomkeshBakhsy looking at your previous query , If you want to save all the rows then your model should be a collection of rows ,So when you save your row using trans_junction you see its a single row which you are saving. – Suraj Singh Jan 27 '16 at 12:03
  • @ByomkeshBakhsy, You need to read my links again. The 'first' one is not a real one and it does not (and should not) post back (its just a template for generating the html). And it done that way specifically so you do not create invalid html (which you are currently doing) –  Jan 27 '16 at 12:05

0 Answers0