1

i have a loop like this in my view that generates the list of student`s names

<Table id="your_table">
<thead>
<tr>
    <th>name</th>
    <th>grade</th>
    <th>remove</th>
</tr>
</thead>
<tbody>
    @foreach (string item in Model)
    {
        <tr>
            <td id="td_name" style="border-left: thin">@item</td>
            <td><input type="text" id="txtGrade_@item" onclick="" style="width: 40px;border-left: thin" /></td>
            <td><input type="checkbox" id="chkStudent_@item" value="@item" /></td>
        </tr>

    }
</tbody>

i use the script below to get the text for the first cell of each row :

$('#btnDone')
          .click(function() {
        //get studentslist
        function getFirstCellTextList(tableElement) {
            if (tableElement instanceof jQuery) {
                // Create an array
                var textList = [];
                // Iterate over each table row
                tableElement.find('tbody tr').each(function () {
                    // Get the first cells's text and push it inside the array
                    var row = $(this);
                    if (row.children('td').length > 0) {
                        textList.push(row.children('td').eq(0).text());
                    }
                });
                return textList;
            }
            return null;
        }
        // Get the array
        var lststudents = getFirstCellTextList($('#your_table'));
        var result = [];
        $(lststudents).each(function(index, item) {
            result.push(item);
        });

       alert(result);
       $.ajax('/TeacherPages/GetGrades/' + result).done(function () {
           alert("done");
       });
    });

the problem is. when i want to send the created array to the controller i get error 404. there is something wrong with this array. because when i manually add values to the array the ajax works without any problems

this is my action:

[AttributeRouting.Web.Mvc.GET("/TeacherPages/GetGrades/{result}")]
    public PartialViewResult GetGrades(string[] result)
    {
        return PartialView();
    }

enter image description here

9
  • don't hardcode url, do like: $.ajax('@Url.Action("GetGrades","TeacherPages")' and show your action how it looks like? Commented Feb 10, 2017 at 12:28
  • You cannot just append a javascript array to a url - you url would need to look like /TeacherPages/GetGrades?name=ABC&name=DEF&name=etc if you method had a parameter `IEnumerable<string> name. What does you array look like? Commented Feb 10, 2017 at 12:31
  • its like : name1.lastname1, name2.lastname2 Commented Feb 10, 2017 at 12:35
  • Why are the dots in there? That's going to cause issues with binding. Can you edit the question to show the value of result Commented Feb 10, 2017 at 12:56
  • dots are there so i can split them after i get the value in my action Commented Feb 10, 2017 at 13:04

1 Answer 1

2

You cannot just append a javascript array to your url. It just converts the values in the array to a comma separated string whereas you need to generate a url which is

/TeacherPages/GetGrades?result=someValue&result=anotherValue&result=etc...

Change your script to

$.ajax({
    url: '@Url.Action("GetGrades", "TeacherPages")', // don't hardcode your url's
    type: 'POST',
    traditional: true,
    data: { result: result },
    success: function (response) {
        ....
    }
});

As a side note, your getFirstCellTextList() function is returning the array you want and its pointless to create another identical array from it. You just need

var result = getFirstCellTextList($('#your_table'));
Sign up to request clarification or add additional context in comments.

1 Comment

thank you. that did it. but is odd. i tried to send information like this to actions in this project in several different occasions with no errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.