0

JavaScript

<script type="text/javascript">
        $(function () {

          $("[ID*=btnAdd]").click(function () {
//Here I am passing routing value i.e student 
            var url = '@Url.Action("GetPV", "Home", new { students=Model.Students })';

            $('#grid1').load(url);
          });

        });

HTML

<div id="grid1"></div>
    <input type="button" id="btn" Value="Submit"/>

MVC Action

//Here iam getting students parameter as null

public ActionResult GetPV(List<Student> students)
        {
            students.Add(new Student());

            StudentModel objstudentmodel = new StudentModel();


            objstudentmodel.StudentList = students;

            return PartialView("_DemoPV", objstudentmodel);

        }

Model

public class StudentModel
    {
        public List<Student> StudentList { get; set; }

    }

    public class Student
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Mobile { get; set; }

        public string Email { get; set; }

        public string Nationality { get; set; }

        public List<HttpPostedFile> files { get; set; }

    }

I want to load the partial view through jquery on button click event.

In this scenario, I want to pass student list as parameter to action method.

Here I am not able to pass the routing values from jquery URL Action to the MVC Action method.

Please assist me to resolve the issue. Thanks.

Pearl
  • 8,373
  • 8
  • 40
  • 59
  • Your'e doing it totally wrong way. First you need to understand how to post data to MVC actions using jquery. May be this link can help you http://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method-using-jquery-ajax#answer-13255459 – Ankush Jain Apr 19 '16 at 04:42
  • You cannot pass a a collection of complex objects to a GET method (well you could if you used `../GetPV&[0].Id=someValue&[0].Name=someValue&...` etc but you would soon exceed the query string limit) –  Apr 19 '16 at 04:42
  • Just get the collection of students again as you did in the method that generated your initial view. –  Apr 19 '16 at 04:45
  • @Ankush: I have also tried that code in the link provided by u, the routing values are passing but not hitting the success function. – Pearl Apr 19 '16 at 04:49

1 Answers1

-1

Try this

    <script type="text/javascript">
            $(function () {
     var _post_Student_list = new Array();

            var templistE = {
                Id: '@Model.Student.Id',
                Name:  '@Model.Student.Name',
                Mobile:  '@Model.Student.Mobile',
                Email:  '@Model.Student.Email',
                Nationality:  '@Model.Student.Nationality'
            }
        _post_Student_list.push(templistE);
   var finalmodel = {

            Student: {
                Id: '@Model.Student.Id', Name:  '@Model.Student.Name', Mobile:  '@Model.Student.Mobile', Email: '@Model.Student.Email', Nationality:  '@Model.Student.Nationality'
            },
            StudentList: _post_Student_list,
        }

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

    $.ajax({
            url: '@Url.Action("GetPV", "Home")',
            data: JSON.stringify({ st: finalmodel }),
            dataType: 'json',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                 $("#grid").load(url);
              }
        });

    });

});
    </script>

<div id="grid"></div>
    <input type="button" id="btnAdd" Value="Submit"/>

public ActionResult GetPV(StudentModel st)
{
    students.Add(st.Student);

    StudentModel objstudentmodel = new StudentModel();


    objstudentmodel.StudentList = st.StudentList;

    return View("~/Views/Home/_DemoPV.cshtml", objstudentmodel);
 }


public class StudentModel
    {
        public StudentModel()
        {

             this.Student = new Student();
        }
        public Student Student { get; set; }
        public List<Student> StudentList { get; set; }
    }
       public class Student
     {
    public int Id { get; set; }

    public string Name { get; set; }

    public string Mobile { get; set; }

    public string Email { get; set; }

    public string Nationality { get; set; }

    public List<HttpPostedFile> files { get; set; }

}
Pawan Lakhara
  • 1,146
  • 4
  • 16
  • 28
  • That will generate `/Home/GetPV/students=System.Collection.Generic.List[Student]` and will not pass the collection of `Student` to the method. –  Apr 19 '16 at 05:24
  • Nope. Still wont work. The model does not contain properties `Id`, `Name` etc. –  Apr 19 '16 at 05:56