Home » Jquery » jquery – How to pass dynamically generated textbox values to the controller from view?-Exceptionshub

jquery – How to pass dynamically generated textbox values to the controller from view?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have generated dynamic textboxes using jQuery on button click. I want to pass their values to the controller using AJAX. I am using the form.serialize method.
i can see the value when i alert .But in the controller it becomes null

$("#add").click(function() {
  $("#mydiv").append($("<div class='row' id='row'> <div class='col-md-4'><div class='form-group'><div class='col-md-10'><input type='text' name='vname[]'class='form-control' placeholder='Vazhipad Name' /></div></div></div><div class='col-md-4'><div class='form-group'><div class='col-md-10'><input type='text' name='vprize[]'class='form-control' placeholder='Vazhipad Prize' /> </div></div> </div><div class='form-group'><div class='col-md-10'><button type='button' id='btnRemove' class='btn btn-primary btnRemove'>-</button></div></div></div></div>"))
});

$("#sub").click(function() {
  $.ajax({
    url: "/Admin/AddVazhipad",
    type: "POST",
    data: $('#myForm').serialize(),
    success: function(data) {
      alert("Success");
    },
    failure: function(data) {
      alert("Failed");
    },
  });
public class Vazhipad
{
  [Required] public int vid { get; set; }
  [Required] public int templeid { get; set; }
  [Required] public List<string> vname { get; set; }
  [Required] public List<float> vprize { get; set; }   
}

The value becomes null in the controller

How to&Answer:

i got the answer.What i did is named the Textbox name “vname[]” to “vname”.It works perfectly now