0

I am developing an MVC4 application with Angular JS and Entity Framework6.0. While saving data to DB its save only in One table. Actually it would be save into different table. Lets say Patient, PatientAddress, City,State etc.

Using EF so its creates Model Like this

    [GeneratedCodeAttribute("EF.Reverse.POCO.Generator", "2.17.0.0")]
    public partial class Patient
    {
        public int PatientId { get; set; } // PatientID (Primary key)
        public Guid? PatientGuid { get; set; } // PatientGUID
        public string FirstName { get; set; } // FirstName
        public string MiddleName { get; set; } // MiddleName
        public string LastName { get; set; } // LastName
        public DateTime? DateOfBirth { get; set; } // DateOfBirth
        public int? Age { get; set; } // Age
        public int GenderId { get; set; } // GenderID
        public string Occupation { get; set; } // Occupation

        // Reverse navigation
        public virtual ICollection<PatientAddress> PatientAddresses { get; set; } // Many to many mapping
        public virtual ICollection<PatientAppointment> PatientAppointments { get; set; } // PatientAppointment.fk_PatientAppointment_Patient
        public virtual ICollection<PatientInsurance> PatientInsurances { get; set; } // Many to many mapping
        public virtual PatientEmergencyContact PatientEmergencyContact { get; set; } // PatientEmergencyContact.fk_PatientEmergencyContact_Patient

        // Foreign keys
        public virtual GenderMaster GenderMaster { get; set; } // fk_Patient_GenderMaster

        public Patient()
        {
            PatientGuid = System.Guid.NewGuid();
            PatientAddresses = new List<PatientAddress>();
            PatientAppointments = new List<PatientAppointment>();
            PatientInsurances = new List<PatientInsurance>();
            InitializePartial();
        }

        partial void InitializePartial();
    }

}

AngularJS Having Controller.Js here I get html form values

app.controller("PatientInfo", function ($scope, myService) {
    $scope.SavePatient = function () {
        debugger;
      //  alert("Red");
        var PatientInformation = {
            FirstName: $scope.Patient.FirstName,
            MiddleName: $scope.Patient.MiddleName,
            LastName: $scope.Patient.LastName,
            DOB: $scope.Patient.DOB,
            Age: $scope.Patient.Age,
            GenderId : $scope.Patient.GenderId,
            Occupation: $scope.Patient.Occupation,
            Address: $scope.PatientAddress.AddressPa,
            District: $scope.CityMaster.NameCm,
            State: $scope.StateMaster.NameSm,
            Pincode: $scope.PatientAddress.PinCodePa,
            PhoneNumber: $scope.PatientAddress.PhoneNumberPa,
            EmailID: $scope.PatientAddress.EmailID,
            ContactPersonName: $scope.ContactPersonName,
            ContactPersonRelation: $scope.ContactPersonRelation,
            ContactPersonAddress: $scope.ContactPersonAddress,
            ContactPersonEmail: $scope.ContactPersonEmail,
            ContactPersonPhoneNumber: $scope.ContactPersonPhoneNumber,
            InsuranceID: $scope.InsuranceID
        };
        var getData = myService.AddPatient(PatientInformation);
        getData.then(function (msg) {
            alert(msg.data);
        }, function () {
            alert('Error in adding record');
        });
    }
});

Service.JS: and here I pass above controller value to MVC Controller.

app.service("myService", function ($http) {
    this.AddPatient = function (PatientInformation) {
        debugger;
        var response = $http({
            method: "post",
            url: "RegisterNewPatient/AddPatient",
            data: JSON.stringify(PatientInformation),
            dataType: "json"
        });
        return response;
    }
});

RegisterNewPatientController.cs

public class RegisterNewPatientController : Controller
    {
        HospitalTeamWebDeContext objHospitalTeamWebDeContext = new HospitalTeamWebDeContext();
        public ActionResult Index()
        { 
            return View("..//Patient/RegisterNewPatient");
        }

        public string AddPatient(Patient objPatient)
        {
            try
            {
                if (objPatient.GenderId == 0)
                {
                    objPatient.GenderId = 1;
                }

                objHospitalTeamWebDeContext.Patients.Add(objPatient);
                objHospitalTeamWebDeContext.SaveChanges();

            }
            catch
            {
            }
            finally
            {

            }
            return "Patient Saved.";
        }
    }
}

Here is above code.

Now It time to show you the o/p

enter image description here

But In controller.CS file I get an only Patient information but Pateint Address, Patient Emergency Contact are always blank. enter image description here

I have tried lot of approach for it but it wont work.

2
  • try creating a different class then Patient as PatientViewModel and create properties as your view model is returning, in the code its not able to directly bind public virtual ICollection<PatientAddress> PatientAddresses as with data coming from js. Commented Jan 14, 2016 at 14:51
  • I agree with Viplock , This is porpbably a binding issue with your entity class. A better practice would be to use a View Model or DTO (A simple POCO C# object) with no virtuals that reflects your entity and it's propities. Then use something like Automapper to repopulate this to your enitty models. Commented Jan 14, 2016 at 15:01

1 Answer 1

1

In your Patient entity class, PatientAddresses property is a collection of PatientAddress class. But looks like from your client side, you are sending a string value for the PatientAddresses property. You need to make sure you are sending a collection (Array).

var patientInformation = {
   FirstName : "Pritish",
   LastName  : "SomeNiceLastName",
   Age : 23,
   PatientAddresses : [{ AddressLine1 : "4201 State St", City : "Ann Arbor", Zip: 48103}] 

};
// Send this javascript object now.

Assuming your PatientAddress entity class looks like

public class PatientAddress
{
  public string AddressLine1 { set;get;}
  public string City { set;get;}
  public string Zip {set;get;}
}

Also, It is not a good idea to use your Entity classes created by your ORM library in your UI layer. That is more a tightly coupled solution. You should consider using view models/DTO's in your UI layer to make it a more loosely coupled solution which will allow you to easily switch your data access layer without impacting your UI code. Take a look at this post to see how to use view specific view models in the UI layer, read that value and save that to the db.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.