使用Javascript:如何通过模型对象列表迭代 [英] Javascript: How to iterate through list of objects in Model

查看:116
本文介绍了使用Javascript:如何通过模型对象列表迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要得到学生对象,它是在一个视图模型的列表,获取学生的名字然后通过.post的$它们发送到服务器,后者我已经想通了,但我不能图如何通过对象的列表进行迭代。基本上,我有这样的:

so I need to get to fetch the names of students in a list of student object that is in a view's model then send them to the server via $.post, the latter I have figured it out but I can't figure out how to iterate through the list of objects. Basically I have this:

//Student object
public class Student 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //Like a bunch of other attributes here
} 

这是在视图模型:

//StudentSearchResult ViewModel

public class StudentSearchResult {

    public IEnumerable<Student> { get; set;}
}

起初我以为只是发送学生名单对象的,但它可能不会像它捆绑了太多的属性(它给了我这个通知的错误,当我试图发送模型)是个好主意我只真正需要级联的名字和姓氏发送给使用$。员额方法我已经有控制器。我想这样的事情:

At first I though of just sending the student list object as is, but it may not be a good idea as it is bundled with too many attributes (it gave me this 'circular' error when I tried to send the model) and I only really need to send the concatenated FirstName and LastName to the controller using the $.post method I already have. I tried things like these:

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 names of each student in studentsearchresult.

不过,我会得到一个错误,我不存在的话,我怎么能在JavaScript通过我的视图模型对象的列表迭代,访问atributes和将它们连接起来,然后将它们存储阵列上字符串,以便我可以将它发送到控制器?我想象中的控制器看起来像这样

But I'd get an error that "i" doesn't exists so, how can I iterate in javascript through the list of objects in my view model, access the atributes and concatenate them and then store them on an array of strings so that I may send it to the controller? I imagine the controller would look like this

[HttpPost]
public ActionResult StudentSearchResult(/*other stuff I send here, */ string[] studentNames){
   //stuff here

  return View();
}

谢谢!

推荐答案

您那边有一些无效的JavaScript。

You have some invalid javascript over there.

这是使你有一个编译C#code(你缺少属性名)修复您的视图模型首先启动:

First start by fixing your view model so that you have a compiling C# code (you were missing a property name):

public class StudentSearchResult 
{
    public IEnumerable<Student> Students { get; set;}
}

然后假设你的控制器动作发送一个JSON结果给客户端(这确保该视图模型是正确的JSON恩codeD,而且应用程序/ JSON 响应内容类型头中发送):

Then assuming your controller actions sends a JSON result to the client (this ensures that the view model is properly JSON encoded and that the application/json response content type header is sent):

[HttpPost]
public ActionResult StudentSearchResult(/*other stuff I send here, */ string[] studentNames)
{
    StudentSearchResult model = ... //stuff here to populate your view model
    return Json(model);
}

您可以在客户端上使用 $轻松地重复。每个() 功能:

you could easily iterate on the client using the $.each() function:

var studentNames = ['name1', 'name2'];
$.post('/Students/StudentSearchResult', studentNames, function(result) {
    var students = result.Students;
    $.each(students, function() {
        alert('FirstName: ' + this.FirstName + ' LastName:' + this.LastName);
    });
});

甚至是纯醇'循环,如果你preFER:

or even a plain ol' for loop if you prefer:

$.post('/Students/StudentSearchResult', studentNames, function(result) {
    var students = result.Students;
    for (var i = 0; i < students.length; i++) {
        var student = students[i];
        alert('FirstName: ' + student.FirstName + ' LastName:' + student.LastName);
    }
});


更新:

它看起来像我有我通过相信您正在执行一个AJAX请求犯了一个错误。你需要什么,而不是在JavaScript中访问模型属性。下面是可以这样做:

It looks like I have I made a mistake by believing that you were performing an AJAX request. Instead what you need is access the model properties in javascript. Here's how this could be done:

@model StudentSearchResult
<script type="text/javascript">
    var students = @Html.Raw(Json.Encode(Model.Students));
    // students is a javascript array that will look like this:
    // students = [{"FirstName":"fn1","LastName":"ln1"}, {"FirstName":"fn2","LastName":"ln2"}, ...];
    for (var i = 0; i < students.length; i++) {
        var student = students[i];
        alert('FirstName: ' + student.FirstName + ' LastName:' + student.LastName);
    }
</script>

这篇关于使用Javascript:如何通过模型对象列表迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆