无法将路由值从jquery传递到mvc操作方法

时间:2016-04-19 04:35:44

标签: jquery asp.net-mvc-3 asp.net-ajax

的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行动

//这里我将学生参数设为空

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

            StudentModel objstudentmodel = new StudentModel();


            objstudentmodel.StudentList = students;

            return PartialView("_DemoPV", objstudentmodel);

        }

模型

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

    }

我想在按钮点击事件中通过jquery加载局部视图。

在这种情况下,我想将学生列表作为参数传递给操作方法。

这里我无法将路由值从jquery URL Action传递给MVC Action方法。

请协助我解决问题。感谢。

1 个答案:

答案 0 :(得分:-1)

试试这个

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

}