I have a form and I need to append two items that are not in the form scope. I'm using jQuery to do it.
My code is working, however, the second time that I submit the form, I'm getting the values I selected in the first time.
Here is my code:
<script type="text/javascript">
$(document).ready(function () {
$("#btnUpdate").click(function (event) {
var items = [];
$('#params :selected').each(function (i, selected) {
items[i] = $(selected).text();
$(selected).prop("selected", false)
});
//So far, so good. My variable 'items' changes the second time.
//The problem is here:
for (var i = 0; i < items.length; i++) {
var submitVal = items[i];
$('#myForm').append("<input type='hidden' name='param" + i + "' value='" + submitVal + "' />");
}
return true;
});
});
</script>
I guess the second time I'm appending a third and a fourth parameters, which means that I have param0, param1 - from the first time - and param0, param1 again - from the second time.
In the third time I'm appending a fifth and sixth and so on.
On the server side I'm using ASP.NET MVC.
public ActionResult Method(string param0 = "", string param1 = "")
I tried to use the method .submit but I was not successful, that is why I'm using the .click method.
Now I'm trying to append the two parameters, submit the form, and remove them after.
Any ideas on how to proceed? Or any ideas to a better solution?