3

I want to send list array in ExportTo Method, but in ExportTo method student parameter get null value. If I use ajax it will work fine. But have to use window.locatoin for pass list array.

View

            $(document).ready(function () {

            $("#SaveBtn").click(function () {

                var student = {

                    Name: "Tanzid",
                    Department: "CSE"

                };
                var student1 = {

                    Name: "Yasir",
                    Department: "BBA"

                };
                var list = [];

                list.push(student);
                list.push(student1);

               var url = '@Url.Action("ExportTo", "Students")';

               var a = JSON.stringify(list);

               window.location = url + '?' + a;
            });
        })

.
Controller

Public void ExportTo(List<Student> student)
    {




    }
Tanzid Kaiser
  • 49
  • 1
  • 5
  • Try to complete your problem here. – Vijunav Vastivch Dec 18 '17 at 05:55
  • i want to pass an array list from view to controller by windows.location but it send null values. i already send one object and get value i used for $.param and i works but it don't work for list. – Tanzid Kaiser Dec 18 '17 at 05:58
  • Why you dont want to use `ajax`? – Vijunav Vastivch Dec 18 '17 at 05:59
  • i need solution without ajax call – Tanzid Kaiser Dec 18 '17 at 06:03
  • have you tried serializing and passing as a path and using `[FromURi]` in your controller? – Neville Nazerane Dec 18 '17 at 08:54
  • check this https://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-asp-net-web-api – Neville Nazerane Dec 18 '17 at 08:57
  • 2
    You need to send a query string with collection indexers - e.g. `...?[0].Name=someValue&[0].Dept=someValue&[1].Name=someValue&....` (assuming the method has parameter which is `IEnumerable` where `T` contains those properties). But why in the world would you want to do that? - apart from the ugly query string, you will probably exceed the query string limit and throw an exception. –  Dec 18 '17 at 11:06
  • I Agree to @StephenMuecke, theres a limit of a query string when we talk about url parameter. – Vijunav Vastivch Dec 19 '17 at 00:24
  • I want to send list array in ExportTo Method, but in ExportTo method student parameter get null value. If I use ajax it will work fine. but i have to use window.location. because if i use ajax i will get return form ExportTo method but i don't need. so i use window.location and i have to pass array list with it . now again i edit my code please see – Tanzid Kaiser Dec 19 '17 at 06:50

1 Answers1

0

Iv'e tested some scenario of your problem and its work without using ajax:

Commented first your code coz i don't have it.

From Index View:

@{
    ViewBag.Title = "Index";
}
<script src="~/scripts/jquery-1.10.2.js"></script>
<h2>Index</h2>
<script>


     $(document).ready(function () {

    var person = [];
    person.push("Reign");//change like $("#idname").val();
    person.push("John");
    var listOfObjects = [];
    person.forEach(function (names) {
        var singleObj = {}
        singleObj['name'] = names;
        singleObj['dept'] = 'IT Dept.';
        listOfObjects.push(singleObj);
    });

    var url = '@Url.Action("Sample", "Home")';

    window.location = url + '?id=' + JSON.stringify(listOfObjects);
})

HomeController:

 public ActionResult Sample()
    {
       string getval = Request.QueryString["id"];
       string concat = @"{""data"":" + getval + "}";


        ValList vl = new JavaScriptSerializer().Deserialize<ValList>(concat);

        foreach (var item in vl.data)
        {
            Console.WriteLine("dept: {0}, name: {1}", item.dept, item.name);
        }

        return View();
    }


    public class ValList
    {

        public List<Values> data { get; set; }
    }

    public class Values
    {
        public string name { get; set; }
        public string dept { get; set; }
    }

It's all working its just depend on your project code with this:

        //var List = [];
        //var a = {
        //    Name: $("#").val(),
        //    Dept: $("#").val()
        //};
        //List.Push(a);

This is all i got..

It's Tested according to your needs.. Good Luck

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30