[removed] How to iterate through list of objects in Model

前端 未结 4 967
清酒与你
清酒与你 2021-02-14 10:48

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

相关标签:
4条回答
  • 2021-02-14 11:32

    you can use $.each of jquery to iterate the result.

    $.each(yourModel,function(){//do with loop});
    

    and for the error. you made mistake in declaration of loop variable

    for (var i = 0; i < length; i++) 
    
    0 讨论(0)
  • 2021-02-14 11:34

    You have some invalid javascript over there.

    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;}
    }
    

    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);
        });
    });
    

    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);
        }
    });
    

    UPDATE:

    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>
    
    0 讨论(0)
  • 2021-02-14 11:38

    Looks like what you need is to output the user names to the client as JSON? Try this:

    var names = @Model.StudentSearchResult.Select(s => new { s.FirstName, s.LastName }).ToList();
    

    I'm not quite familiar with the Razor syntax, but I think you can still understand the code above.

    0 讨论(0)
  • 2021-02-14 11:40
    for (var i = 0; i < length; i++) 
    

    instead of

    for (int i = 0; i < length; i++) 
    

    should work.

    0 讨论(0)
提交回复
热议问题