按钮点击我发送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);
}
$('.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;
}
{"StudentListViewModel":{"page":"1","sort":"ID","sortdir":"Ascending","Students":[{"ID":"2","FirstName":"joydip","sen":"Bhattacharjee",
"IsActive":true,"StateID":"3","CityID":"4"}]}}