[英]How to post List of List from view to Controller in asp .net Core?
I want to Post List of data by Ajax to asp .net controller.我想通过 Ajax 将数据列表发布到 asp .net 控制器。
var DataList = { motherName: $("#motherName").val(), parentName: parentName, pieceName: pieceName, pieceFeatures: pieceFeatures, //pieceFeaturesList: null, featureValueLists: featureValueLists }; JQAjax("Post", "Add_KbyFeatures", DataList , function (data) { if (data) if (data.isSuccess) { //// } });
featureValueLists is a list. featureValueLists 是一个列表。
$("#your_button_id").on('click', function() { //if your button got clicked. This button going to get triggered
if($("#your_button_id").on(':clicked')) {
$.ajax({
url: //Your URL to asp.net controller,
type: "GET", //GET means getting the data, POST saving the data
data: {
motherName: $("#motherName").val(),
parentName: parentName,
pieceName: pieceName,
pieceFeatures: pieceFeatures,
//pieceFeaturesList: null,
featureValueLists: featureValueLists
}, //This is where you get the value for your controller
dataType: "html", //dataType is the type if your result should be json or html
success: function(result) { //if the controller gets the data and its not null or undefined it will throw it here
$("your_result_show").html(result) //after the controller gets your data. The gathered data will show here
}
})
}
})
Below is a demo, you can refer to it:下面是一个demo,大家可以参考一下:
Thing:事物:
public class Thing
{
public string parentName { get; set; }
public string pieceName { get; set; }
public string pieceFeatures { get; set; }
}
HomeController:家庭控制器:
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public void PassThings(IEnumerable<Thing> things)
{
// do stuff with things here...
}
}
Index view:索引视图:
<input type="button" id="btnProjectSave" name="but1" class="btn btn-default" value ="OnPostProjectSubmit">
<div id="result">
</div>
@section Scripts{
<script>
$(function() {
$("#btnProjectSave").click(function(e) {
e.preventDefault();
var things = [
{
parentName: 'parentName',
pieceName: 'pieceName',
pieceFeatures: 'pieceFeatures',//do your staff...
},
];
$.post('@Url.Action("PassThings")', { things: things },
function() {
$('#result').html('"PassThings()" successfully called.');
});
});
});
</script>
}
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.