json没有反序列化来查看ASP.Net MVC中的模型

时间:2016-01-05 14:58:14

标签: json asp.net-mvc

按钮点击我发送json数据到动作方法,从那里我看到学生数组没有填充json数据。

在这里,我将发布我的视图模型和模型代码

public class StudentListViewModel
{
    public int StartIndex { get; set; }
    public int EndIndex { get; set; }
    public int page { get; set; }
    public int RowCount { get; set; }
    public int PageSize { get; set; }
    public int CurrentPage { get; set; }

    public string sort { get; set; }
    public string sortdir { get; set; }

    public IList<Student> Students { get; set; }

    public int SelectedStateId { set; get; }
    public IList<State> States { get; set; }

    public int SelectedCityId { set; get; }
    public IList<City> Cities { get; set; }

    public StudentListViewModel()
    {
        PageSize = 5;
        sort = "ID";
        sortdir = "ASC";
        CurrentPage = 1;
    }

    public void SetUpParams(StudentListViewModel oSVm)
    {
        if (oSVm.page == 0)
            oSVm.page = 1;

        StartIndex = ((oSVm.page * oSVm.PageSize) - oSVm.PageSize) + 1;
        EndIndex = (oSVm.page * oSVm.PageSize);
        CurrentPage = StartIndex;

        if (string.IsNullOrEmpty(oSVm.sort))
            oSVm.sort = "ID";

        if (string.IsNullOrEmpty(oSVm.sortdir))
            oSVm.sortdir = "ASC";
    }
}

学生模特

public class Student
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsActive { get; set; }
    public int StateID { get; set; }
    public string StateName { get; set; }
    public int CityID { get; set; }
    public string CityName { get; set; }
}

我的行动方法

public ActionResult UpdateStudents(StudentListViewModel oSVm)
{
    System.Threading.Thread.Sleep(1000); // just simulate delay of one second
    StudentListViewModel SVm = new StudentListViewModel();
    //SVm.SetUpParams(oSVm);
    SVm.Students = _Studentdata.SaveXML(SVm.Students.ToXml("Students"), SVm.StartIndex, SVm.EndIndex, SVm.sort, oSVm.sortdir).ToList();
    return View("ListStudents", SVm);
}

这是生成json和post to action

的jquery代码
$('.save-user').on('click', function () {
    var tr = $(this).parents('tr:first');

    var Sortdir = $("#dir").val();
    var Sortcol = $("#col").val();
    var page = $("#page").val();

    var ID = tr.find("input[id*='HiddenID']").val();
    var FirstName = tr.find("input[id*='txtFirstName']").val();
    var LastName = tr.find("input[id*='txtLastName']").val();
    var StateID = tr.find("select[id*='cboState'] :selected").val();
    var CityID = tr.find("select[id*='cboCity'] :selected").val();
    var IsActive = $("[class*='box']").is(':checked');

    var data = new Object();
    var StudentArray = [];
    StudentArray.push(PopulateStudent(ID, FirstName, LastName, StateID, CityID, IsActive));

    data.page = page;
    data.sort = Sortcol;
    data.sortdir = Sortdir;
    data.Students = StudentArray;

    tr.find('.edit-mode, .display-mode').toggle();
    if ($(tr).find("td:nth-child(2)").hasClass('PadOff')) {
        $(tr).find("td:nth-child(2)").removeClass("PadOff");
        $(tr).find("td:nth-child(3)").removeClass("PadOff");
        $(tr).find("td:nth-child(4)").removeClass("PadOff");
        $(tr).find("td:nth-child(5)").removeClass("PadOff");

    }

    $(tr).find("td:nth-child(2)").addClass("PadOn");
    $(tr).find("td:nth-child(3)").addClass("PadOn");
    $(tr).find("td:nth-child(4)").addClass("PadOn");
    $(tr).find("td:nth-child(5)").addClass("PadOn");

    alert(JSON.stringify({ StudentListViewModel: data }));

    $.ajax({
        url:  '@Url.Action("UpdateStudents", "Student")',
        data: JSON.stringify({ StudentListViewModel: data }),
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert(data);
        }
    });


    return false;

});

function PopulateStudent(id, firstname, lastname, stateid, cityid, isactive) {
    var Student = new Object();
    Student.ID = id;
    Student.FirstName = firstname;
    Student.LastName = lastname;
    Student.IsActive = isactive;
    Student.StateID = stateid;
    Student.CityID = cityid;
    return Student;
}

json看起来像

{"StudentListViewModel":{"page":"1","sort":"ID","sortdir":"Ascending","Students":[{"ID":"2","FirstName":"joydip","sen":"Bhattacharjee",
"IsActive":true,"StateID":"3","CityID":"4"}]}}

0 个答案:

没有答案