I'm trying to create a model with dynamic fields... in essence, my page has a few buttons that add text boxes.
I have a variable - "counter"
@{
ViewBag.Title = "Create";
var counter = 0;
}
I want to increase it everytime my button is clicked that adds a field..
<div class="form-group">
<button class="control-label col-md-2" id="btnAddFieldTwo">Add Question with Multiple Answers</button>
<div class="col-md-10">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div id="fields2"></div>
}
<div style="color:blue"><b>Data:</b> @ViewBag.Data</div>
<!-- JS includes -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var $fields = $('#fields2');
$('#btnAddFieldTwo').click(function (e) {
e.preventDefault();
@{ counter++ }
$(' <input type="text" class="form-control" name="dynamicFieldTwo">Enter the question</input><br/>@Html.TextBox("Question",@counter, new { style="color:red" }) ').appendTo($fields);
});
});
</script>
</div>
</div>
I figured adding @{ counter++} right above the line that adds the dynamic text field would work. But the counter variable is only increased once no matter how many times I click the button. How do I increase counter every time the button is clicked ?