I am trying to get multiple entries from my controller using Ajax. However, both id and qty return null (I gave them default values). Please review my code below.
<script type="text/javascript">
$(document).ready(function () {
$("#submit").on("click", function () {
var elem = {};
elem.qty = "10";
elem.id = 1;
$.ajax({
url: '/Home/Form1/',
type: 'POST',
dataType: 'json',
data: JSON.stringify(elem),
success: function (data) {
alert(data);
console.log(data);
$("#output").html(data[0]);
}
});
});
});
public class Data
{
public string qty { get; set; }
public int? id { get; set; }
}
public ActionResult Form1(Data elem)
{
List<Data> Elements = new List<Data>
{
new Data {qty=elem.qty, id=elem.id},
};
return Json(Elements);
}

HTMLelements as well to that it can be reproduced easily.@model WebApplication3.Models.Data <table> <tr> <td><input type="text" id="Id" /></td> </tr> <tr> <td colspan="2"><button class="btn btn-primary" id="submit" value="submit">Submit</button></td> </tr> </table> <br /> <br /> <h5 style="color:purple" id="output"></h5>var elem = {};isn't type ofDataclass. Thats why you get null in backend.