使用Knockout 3.3js与Asp.net MVC 5时,大型viewmodel崩溃

时间:2015-05-09 07:14:56

标签: twitter-bootstrap asp.net-mvc-4 knockout.js

我是asp.net MVC&的新手。昏死。 我需要在网格中显示患者详细信息,同时添加患者。

患者显示器以敲除表格式显示大约10条记录,它位于页面的下半部分。 患者插入/更新是构成页面上半部分的表格。

当我以前使用淘汰赛保存/编辑患者时,页面错误显示Web服务器返回错误。 当我在表格中只丢失3条记录时,保存/编辑工作正常。

代码如下, 客户端ViewModel

  self.save = function () {
    ko.utils.arrayForEach(self.PatientDisplay(), function (pt) {
        pt.DateOfBirth(moment(pt.DateOfBirth()));
        pt.RegdDate(moment(pt.RegdDate()));
    });
    self.PatientModel.RegdDate(moment(self.PatientModel.RegdDate()).format('DD/MM/YYYY'));
    //self.PatientModel.DateOfBirth(moment(self.PatientModel.RegdDate()).format('DD/MM/YYYY'));
    $.ajax({
        url: "/Patient/Save/",
        method: "POST",
        data: ko.toJSON(self.PatientModel),
        contentType: "application/json",
        success: function (data) {

            ko.mapping.fromJS(data, {}, self);
            self.resetForm();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 400) {
                $('#MessageToClient').text(XMLHttpRequest.responseText);
            }
            else {
                $('#MessageToClient').text('The Web Server had an Error');
            }

        }
    })
}

服务器ViewModel

        public PatientViewModel PatientModel { get; set; }
    public string MessageToClient { get; set; }
    public List<PatientViewModel> PatientDisplay { get; set; }
    public AddressViewModel AddressModel { get; set; }
    public FeeStructureViewModel FeesModel { get; set; }
    public EmployeeViewModel EmployeeModel { get; set; }
    public List<PatientTypeViewModel> PatientType { get; set; }
    public List<BloodGroup> BloodGroupModel { get; set; }
    public List<Gender> GenderModel { get; set; }
    public List<EmployeeViewModel> DocList { get; set; }
    public List<int> DeletePatients { get; set; }

    public PatientEditViewModel()
    {

        PatientModel = new PatientViewModel();
        AddressModel = new AddressViewModel();
        FeesModel = new FeeStructureViewModel();
        EmployeeModel = new EmployeeViewModel();
        PatientType = new List<PatientTypeViewModel>();
        GenderModel = new PatientExtras().GetAllGenders();
        BloodGroupModel = new PatientExtras().GetBloodGroups();
        DocList = new List<EmployeeViewModel>();
        PatientDisplay = new List<PatientViewModel>();
        DeletePatients = new List<int>();
    }
}

Patient ViewModel

public PatientViewModel()
    {
        FeeStructure = new List<FeeStructureViewModel>();
        Address = new List<AddressViewModel>();
        Employee = new EmployeeViewModel();
        FeesModel = new FeeStructureViewModel();
        AddressModel = new AddressViewModel();
        MaritalStatus = "Married";
        this.DateOfBirth = DateTime.Now;

        this.RegdDate = DateTime.Now;
    }

    public FeeStructureViewModel FeesModel { get; set; }
    public AddressViewModel AddressModel { get; set; }
    public int PatientId { get; set; }
    public int PatientTypeId { get; set; }
    public int EmpId { get; set; }
    public EmployeeViewModel Employee { get; set; }
    public List<FeeStructureViewModel> FeeStructure { get; set; }
    public List<AddressViewModel> Address { get; set; }

    [Required(ErrorMessage = "Server: Please Enter a valid First Name")]
    [StringLength(50, ErrorMessage = "Server: Please enter no more than 50 Characters")]
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public string Occupation { get; set; }
    public DateTime DateOfBirth { get; set; }
    public DateTime RegdDate { get; set; }
    public string BloodGroup { get; set; }
    public string MaritalStatus { get; set; }
    public bool IsActive { get; set; }
    public int CreatedBy { get; set; }
    public int UpdatedBy { get; set; }
    public ObjectState ObjectState { get; set; }

控制器

索引方法

var employees = hmsDbContext.Employees.ToList();
        var patientTypes = hmsDbContext.PatientTypes.ToList();
        var patients = hmsDbContext.Patients.ToList();
        var departments = hmsDbContext.Departments.ToList();

        employees.ForEach(x =>
        {
            var dept = departments.Where(d => d.DepartmentId == x.DepartmentId).Single();
            var emp = Converter.ConvertToEmployeeVM(x);
            emp.Department = Converter.ConvertToDepartmentVM(dept);
            patientEditViewModel.DocList.Add(emp);
        });

        patientTypes.ForEach(pt =>
        {
            patientEditViewModel.PatientType.Add(Converter.ConvertToPatientTypeVM(pt));
        });

        var patientDisplay = hmsDbContext.Patients.ToList().OrderByDescending(x => x.RegdDate).Take(3).ToList();

        patientDisplay.ForEach(p =>
        {
            var _patients = Converter.ConvertToPatientVM(p);
            _patients.Employee = Converter.ConvertToEmployeeVM(employees.Where(x => x.EmpId == p.EmpId).Single());

            patientEditViewModel.PatientDisplay.Add(_patients);
        });

保存方法

  public JsonResult Save(PatientEditViewModel patientEditViewModel)
    {


        if (!ModelState.IsValid)
        {
            throw new ModelStateExceptions(ModelState);
        }

        var patients = new Patient();
        patients = Converter.ConvertToPatient(patientEditViewModel.PatientModel);

        var message = string.Empty;
        //string.Format("{0}'s Record has been Registered Successfully!", patientEditViewModel.PatientModel.FirstName + " " + patientEditViewModel.PatientModel.LastName);

        hmsDbContext.Patients.Attach(patients);
        hmsDbContext.ApplyStateChanges();

请让我知道如何在一页中显示10条记录并保存一条记录。

任何帮助表示赞赏。

0 个答案:

没有答案