0

I have generic list to view.

List<Student> StudentList = ViewBag.StudentList;

I want send this list with JSON.

var listSTD = @StudentList;
var id = 5;
    $.ajax({
        url: "/Students/Check",
        type: "POST",
        dataType: 'json',
        data: {"listSTD": listSTD, "id": id},
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            alert(result.Result);
        }
    });

but is not working. get error in this code:

var listSTD = @StudentList;

2
  • and the error is.....? Commented Dec 4, 2019 at 12:39
  • = @StudentList; is clearly invalid. The view has access to the model, the Temp dictionary and the ViewBag. If you want to access something from the ViewBag use the same syntax you use in C#, eg @ViewBag.StudentList. Everything after that @ is C# code Commented Dec 4, 2019 at 13:11

2 Answers 2

1

You shouldn't be passing the List<Student object directly to your script as the object is not in a format js can read or work with so you need to JSON serialize the object into a JSON string which you can then include in the data body of your post request.

You can serialize the object with Newtonsoft.Json and you need to make sure the listSTD variable is instantiated inline when the view is rendered rather than in an external file (the rest of your code can be in an external js file). For example:

<html>
  <body>
    @{ List<Student> StudentList = ViewBag.StudentList; }

    <script>
       var listSTD = "@JsonConvert.SerializeObject(StudentList)";
    </script>
  </body>
<html>

On the server-side controller action responding to your post request at /Students/Check you can de-serialize the posted student list JSON back into List<Student> For example in your controller action:

public class StudentsController
{
  public ActionResult Check(StudentsCheckViewModel model)
  {
     int id = model.id;
     List<Student> Students = Json.DeserializeObject<List<Student>>(model.listSTD);
  }
}

And the view model:

public class StudentsCheckViewModel 
{
   public int id { get; set; }

   public string listSTD { get; set; }
}
Sign up to request clarification or add additional context in comments.

7 Comments

What is the json action input with this code? i using this but send null list in action.
@mishicaro you didn't ask anything about actions. You asked how to emit a JSON array into the view
Do you have Newtonsoft.Json? If not, the resulting serialized JSON string will probably null or error. Install in nuget by: Install-Package Newtonsoft.Json
I want use this list in input action in /Students/Check.
not error in code. but send or give empty null list. i use List<Student> type in input json action in "/Students/Check" address
|
0

You can serialize the data as JSON into the view using the JSON helper, eg :

<script type="text/javascript" charset="utf-8">

    var rowData = @Json.Serialize(Model.Rows);
    ...

</script>

In older versions you may have to use Json.Encode or wrap the call in Html.Raw, eg @Html.Raw(Json.Serialize(...))

3 Comments

I do not have Serialize method. and use Encode but not working...
@mishicaro I copied that code from my own project. Which ASP.NET MVC version are you using and what does not working mean? What do you see in the page source?
@mishicaro ASP.NET MVC 6 does have Json.Serialize as shown in this possibly duplicate question

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.