0

I have one ViewModel that contains three Collection of ExternalProjectViewModel, CertificateUniverSityViewModel, CertificateInstitutionsViewModel.

CreateFreelancerProfileViewModel.cs

public class CreateFreelancerProfileViewModel : BaseViewModel
{
    // More ...
   public List<ExternalProjectViewModel> ExternalProjects { get; set; }   

   public List<CertificateUniverSityViewModel> CertificateUniverSitys { get; set; }

   public List<CertificateInstitutionsViewModel> CertificateInstitutions { get; set; }

}

My Ajax code:

            $('#Controller').on('click','#SaveProfile',

            function() {

                debugger;
                var CertificateInstitutions = 
                    JSON.parse(localStorage.getItem("CertificateInstitutionsListLocal"));

                var CertificateUniverSitys = 
                    JSON.parse(localStorage.getItem("CertificateUniverSitysListLocal"));

                var ExternalProjects = 
                    JSON.parse(localStorage.getItem("ExProjectListLocal"));

                $.ajax({
                    url : '@Url.Action(MVC.Freelancer.Profile.CreatePrfile())',
                    method: "POST",
                    data: {
                        ExternalProjects,
                        CertificateUniverSitys,
                        CertificateInstitutions
                    }
                });


            });

When I Want Send Objects to Controller, First Get It from LocalStorage And After Get it Send it to Controller Action:

 public virtual ActionResult CreatePrfile(CreateFreelancerProfileViewModel viewModel)

When I see viewModel Values Show My Objects Count That is 2 but Objects Properties is null.so that my server object properties name equally with the client Object properties name.

LocalStorage Values

[{"ExternalProjects":{"Name":"wqeqwe","Body":"wqewqe","Url":‌​"wqewqe"}}]

[{"CertificateUniverSity":{"Name":"sad","Description":"sadas‌​","DateOfGets":"sad"‌​,"CertificateType":"‌​2","Field":"sadasd",‌​"UniName":"sad","Cer‌​tificateUniverSityLe‌​vel":"2"}}]
2
  • capture that post request in your browser and see Commented Jan 24, 2017 at 15:33
  • @Steve I do This Work in client side object properties is fill Commented Jan 24, 2017 at 15:34

1 Answer 1

1

You could send them as JSON payload:

$.ajax({
    url : '@Url.Action(MVC.Freelancer.Profile.CreatePrfile())',
    method: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({
        externalProjects: ExternalProjects,
        certificateUniverSitys: CertificateUniverSitys,
        certificateInstitutions: CertificateInstitutions
    }),
    success: function(result) {
        alert('data sent successfully');
    }
});

This assumes that the instances you got from the localStorage of those 3 variables represent javascript arrays with the corresponding objects in it.

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

10 Comments

I try Your Code But it does not change
Could you show the exact payload that gets sent to the server? I suspect that the ExternalProjects, CertificateUniverSitys and CertificateInstitutions that you got from the local storage do not represent javascript arrays.
I'd guess we the case should match the class properties. Try using ExternalProjects: ExternalProjects, instead of externalProjects: ExternalProjects, (this is just a shot in the dark).
No, the JSON serializer will handle the casing properly.
@DarinDimitrov [{"ExternalProjects":{"Name":"wqeqwe","Body":"wqewqe","Url":"wqewqe"}}]
|

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.