小编Ric*_*rdo的帖子

Javascript:如何遍历模型中的对象列表

所以我需要在视图模型中的学生对象列表中获取学生的名字,然后通过$ .post将它们发送到服务器,后者我已经弄明白但我无法弄清楚如何遍历对象列表.基本上我有这个:

//Student object
public class Student 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //Like a bunch of other attributes here
} 
Run Code Online (Sandbox Code Playgroud)

这是视图中的模型:

//StudentSearchResult ViewModel

public class StudentSearchResult {

    public IEnumerable<Student> { get; set;}
}
Run Code Online (Sandbox Code Playgroud)

起初我虽然只是按原样发送学生列表对象,但它可能不是一个好主意,因为它捆绑了太多属性(当我尝试发送模型时它给了我'循环'错误)而且我只真的需要使用我已经拥有的$ .post方法将连接的FirstName和LastName发送到控制器.我尝试过这样的事情:

var names = []  
var length = "@Model.StudentSearchResult.count()";  
for (int i = 0; i < length; i++) 
{
     names[] = "@Model.StudentSearchResult[i].Name + @Model.StudentSearchResult[i].LastName"
}
//$.post function here that calls the controller and send the concatenated …
Run Code Online (Sandbox Code Playgroud)

javascript c# asp.net-mvc razor

9
推荐指数
1
解决办法
4万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

javascript ×1

razor ×1