简体   繁体   English

如何在 asp .net Core 中将列表列表从视图发布到控制器?

[英]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.

相关问题 How to POST javascript object, javascript object list and file list to asp.net core controller - How to POST javascript object, javascript object list and file list to asp.net core controller ASP .NET Core MVC-将HTML选择控件中的列表数据发布到MVC控制器中 - ASP .NET Core MVC - Post List data from HTML select control into MVC Controller 在asp.net core 2.2中从视图到控制器发布位于视图模型内的对象列表 - Posting a list of object which is located inside a viewmodel from view to controller in asp.net core 2.2 ASP.NET:开机自检列表 <Object> /使用Ajax(JSON)从视图到控制器进行建模 - ASP.NET: POST List<Object> / Model with Ajax (JSON) from view to controller 如何从 AJAX 方法返回列表以从 controller ASP.NET MVC 查看 - How to return a list from AJAX method to view from controller ASP.NET MVC 如何在ASP.NET MVC中使用Razor View从对象列表中获取控制器中的单个对象 - How to get Single Object in Controller from List of Objects using Razor View in ASP.NET MVC Pro Asp.net core MVC 如何创建视图列表并将数据列表传递给控制器​​(如何将数据列表从视图传递给控制器​​) - Pro Asp.net core MVC how create list of view and pass the data list to the controllers (How can i pass list of data from view to controllers) 数据返回 null 从视图到 ASP.NET 内核中的 controller - Data return null from view to controller in ASP.NET Core 如何使用asp net core 3.1通过Ajax将对象列表发送到controller? - How to send a list of objects to controller via Ajax using asp net core 3.1? ASP.NET MVC - 控制器使用 Html.BeginForm() 从视图接收列表错误 - ASP.NET MVC - Controller receives the list incorret from view using Html.BeginForm()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM