0

I have the following entities in my model.

 public class Provider
{
    public int ProviderId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SSN { get; set; }
    public string NPI { get; set; }
    public ProviderDetails ProviderDetails { get; set; }

}

public class ProviderDetails
{
    public int ProviderDetailsId { get; set; }
    public string Certification { get; set; }
    public string Specialization { get; set; }
    public string TaxonomyCode { get; set; }
    public string ContactNumber { get; set; }
    public string ContactEmail { get; set; }
    public int ProviderId { get; set; }
}

I have the following controller action method.

 [HttpPost]
    public ActionResult CreateProvider(Provider provider)
    {
        try
        {
            int providerCreationSuccessful = _repository.CreateProvider(provider);
            if (providerCreationSuccessful == 1)
                TempData["userIntimation"] = "Provider Registered Successfully";

            return RedirectToAction("ShowTheListOfProviders");
        }
        catch (Exception Ex)
        {
            _logger.Error(Ex.Message);
            return View("Error");
        }
    }

I am sending data to the controller using AJAX as shown.

self.createProviderDetails = function () {
    $.ajax({
        url: "/Provider/CreateProvider/",
        type: "POST",
        data: fillModel(),
        async: false,
        success: function (result) {
            if (result.url) {
                location.href = result.url;
            }
        }
    }).fail(
             function (xhr, textStatus, err) {
                 alert(err);
             });
};

fillmodel function is

var fillModel = function () {
    var providerData =
        {
            ProviderId: self.providerID(),
            FirstName: self.firstName(),
            LastName: self.lastName(),
            SSN: self.SSN(),
            NPI: self.NPI(),
            ProviderDetails: {
                ProviderDetailsId: 0,
                Certification: self.certification(),
                Specialization: self.specialization(),
                TaxonomyCode: self.taxonomyCode(),
                ContactNumber: self.contactNumber(),
                ContactEmail: self.contactEmail(),
                ProviderId: self.providerID()          
            }   
        }
    return providerData;
}

The object data is fine on the Javascript side but at the controller,the nested objects are null as shown here.

enter image description here

Please let me know,As to what I am doing wrong.I am not able to figure this one out.

2
  • 1
    Content-Type:application/json add this to ajax request Commented Feb 23, 2015 at 11:57
  • 1
    The DefaultModelBinder will bind correctly if the names are in the following format ProviderDetails.ProviderDetailsId: 0, ProviderDetails.Certification: someValue etc (dot notation). But if you property construct your view using the strongly typed helpers, all you need is data: $(yourForm).serialize(), Commented Feb 23, 2015 at 12:06

4 Answers 4

1

Pass the same name of columns and properties then pass into model then should be visible in you model when you pass into the controller ,

data: JSON.stringify({
     model: {
                 "Column1": $("#Column1").val(),
                 "Column2": $("#Column2").val(),
                 "Column3": $("#Column3").val(),
                 "Column4": $("#Column4").val(),
           }),

and pass the model like this because last time have find this type issue then i have fixed through this.

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

4 Comments

What about the nested model?Thats the one which is null.
same implementation required ,make nested model in below column4 and map the same name model and as well column name it should be pass , if raise same issue see your coding with master mind.
Should I change the controller if I do like this or is some change required in the AJAX code.
no changes required to passing you model to controller.
0
 self.createProviderDetails = function () {
        $.ajax({
            url: "/Provider/CreateProvider/",
            type: "POST",
            data: fillModel(),
            async: false,
datatype : "json",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                if (result.url) {
                    location.href = result.url;
                }
            }
        }).fail(
                 function (xhr, textStatus, err) {
                     alert(err);
                 });
    };

Comments

0

Try specifying the dataType for your AJAX request as 'json':

self.createProviderDetails = function () {
    $.ajax({
        url: "/Provider/CreateProvider/",
        type: "POST",
        data: fillModel(),
        dataType: "json", // specify data type
        async: false,
        success: function (result) {
            if (result.url) {
                location.href = result.url;
            }
        }
    }).fail(
             function (xhr, textStatus, err) {
                 alert(err);
             });
};

Comments

0

ANOTHER very common error are square brackets []. Be careful when you are building your nested object.

 var object = {
     NestedObject: [{ Column1: "value1" }], //error
     NestedObject: { Column1: "value1" }, // Good.
 }

there is no difference if you use quotes for Name's columns and nested columns:

 var object = {
     "NestedObject": { "Column1": "value1" }, // Good.
 }

You'll have your object in the controller. But anyway, The most important thing is specify datatype: JSON

 $.ajax({
    url: "/Provider/CreateProvider/",
    type: "POST",
    data: fillModel(),
    dataType: "json", // specify data type JSON
    success: function (result) {

    }
})

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.