Javascript 如何将对象列表从视图中的java脚本代码传递到控制器中的操作方法

Javascript 如何将对象列表从视图中的java脚本代码传递到控制器中的操作方法,javascript,c#,model-view-controller,Javascript,C#,Model View Controller,我在javascript编程方面没有太多经验。我想将对象列表从“视图”中的javascript代码传递到MVC“控制器”中的操作方法。 以下是我努力实现的目标—— MVC模型和控制器: Public Class Student { Public int Id {get; set;} Public string Name {get; set;} Public string Address {get; set;} } Pub

我在javascript编程方面没有太多经验。我想将对象列表从“视图”中的javascript代码传递到MVC“控制器”中的操作方法。 以下是我努力实现的目标——

MVC模型和控制器:

 Public Class Student
    {
        Public int Id {get; set;}
        Public string Name {get; set;}
        Public string Address {get; set;}
    }

    Public class StudentController : Controller
     {
        Public ActionResult Index()
        {
            // Read the data from database, create a list of “Student”
            return View(“Display”, StudentList);
        }

        [HttpPost]
        Public ActionResult OrderStudentList(List<Student> StudentList)
        {
            // Code to ascend/descend list
        }
     }
什么都没起作用。如果有人帮忙,我会非常感激。 谢谢

学生姓名
//这是jquery,要使用$syntax,必须包含它。它是可用的 //在项目目录中,您只需检查并更新以下行。


函数myFunction(){
var studentL=@Html.Raw(Json.Encode(Model));
$.ajax({
url:“@url.Action”(“OrderStudentList”,“Student”)”,
类型:“POST”,
contentType:“应用程序/json”,
数据:JSON.stringify({StudentList:studentL}),
成功:功能(响应){
响应?警报(“它起作用了!”):警报(“它没起作用”);
}
});
}   

我不太确定您希望这一行做什么:

var studentL = @Model.ToList();
但无论如何,您需要立即将模型转换为JSON,您的Javascript函数需要如下所示:

function myFunction() {
    $.ajax({
        url: "@Url.Action("OrderStudentList", "Student")",
        type: "POST",
        contentType: "application/json",
        data: '@Html.Raw(JsonConvert.SerializeObject(Model.ToList()))',
        success: function (response) {
            response ? alert("It worked!") : alert("It didn't work.");
        }
    });
} 
您的操作方法需要具有以下签名:

    [HttpPost]
    Public ActionResult OrderStudentList(List<Student> model)
    {
        // Code to ascend/descend list
    }
[HttpPost]
Public ActionResult OrderStudentList(列表模型)
{
//上升/下降列表的代码
}

您是否尝试过将
数据
对象用字符串引号括起来<代码>数据:“JSON.stringify({StudentList:studentL})”,如果我这样做,在单击按钮后什么也不会发生。我尝试了列表和数组。在运行时,行var studentArr=@Model.ToArray()变为:var studentArr=Student[];抛出错误,“Javascript严重错误,语法错误”非常感谢John M先生。这解决了我的问题。
 Converted a list to an array and tried to copy each element of a list 
   individually to an element of javascript array as follows
        var arrayJs = [];
        for(var i=0; i<= @Model.Count(); i++)
        {
            arrayJs.push(@Model.ElementAt(i));
        }
   But it gives the syntax error with the “i does not exist in 
   the current context”.
 Created a ViewModel with Student list as a property
        Class StudentList
        {
            Public List<Student> StudentList {get; set;}
        }
   Tried passing an instance of a viewModel as a JSON object to action  
   method “OrderStudentList”.
<button onclick="myFunction()">Student Name</button>
 <script type="text/javascript">   
    function myFunction() {
        var studentL =  @Html.Raw(Json.Encode(Model));

        $.ajax({
            url: "@Url.Action("OrderStudentList", "Student")",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify({ StudentList: studentL }),
            success: function (response) {
                response ? alert("It worked!") : alert("It didn't work.");
            }
        });
    }   
var studentL = @Model.ToList();
function myFunction() {
    $.ajax({
        url: "@Url.Action("OrderStudentList", "Student")",
        type: "POST",
        contentType: "application/json",
        data: '@Html.Raw(JsonConvert.SerializeObject(Model.ToList()))',
        success: function (response) {
            response ? alert("It worked!") : alert("It didn't work.");
        }
    });
} 
    [HttpPost]
    Public ActionResult OrderStudentList(List<Student> model)
    {
        // Code to ascend/descend list
    }