Jose
Jose

Reputation: 1140

HTML elements not updating from ajax call

I am trying to update some input fields on my page after performing an ajax call that returns an object. I know the call is being made and returning the correct items, however, it is not writing those items on the textboxes. This is how I'm setting them up after the ajax call:

success: function(data) {
         $('#address_LocaleID').val(data.LocaleID);
         $('#address_StreetAddress').val(data.StreetAddress);
         $('#address_Address2').val(data.Address2);
         $('#address_StreetNumber').val(data.StreetNumber);
         $('#address_BuildingNumber').val(data.BuildingNumber);
         $('#address_City').val(data.City);
         $('#address_StateProvinceID').val(data.StateProvinceID);
         $('#address_CountryID').val(data.CountryID);
         $('#address_CountyID').val(data.CountyID);
         $('#address_PostalCode').val(data.PostalCode);
         $('#address_ApplicationTypeID').val(data.ApplicationTypeID);
         $('#address_MapURL').val(data.MapURL);
         $('#address_Description').val(data.Description);
         $('#address_IsActive').val(data.IsActive);
         $('#address_DateCreated').val(data.DateCreated);
}

The 'data' parameter being brought back is an "Address" object from a controller. I believe this is where the problem lies, I'm returning a C# object, but say for example, I create an alert inside the success function to spit out 'data.City', it gives me the correct value. Where could the error lie?

Thanks for your help.

EDIT:

Action method:

        [HttpGet]
    public Address AddressGet(string guid, string id)
    {
        Guid addressID = new Guid(guid);
        Address address = new Address();
        address.LocaleID = new Guid(id);

        if (addressID == Guid.Empty)
        {
            return address;
        }
        else
        {
            address = _iRepository.Address_Get(addressID);

            return address;
        }
    }

There is nothing special being done, just returning an Address object.

Upvotes: 0

Views: 691

Answers (2)

SteveKB
SteveKB

Reputation: 190

There is probably something from with your dom lookup make sure all the names are correct.

Also you don't tell us things like what the dom object that should be changed look like. we don't need to see your return json since it's working in the alert but I think the problem lies in when it's trying to change the values in the dom.

also make sure that the ajax call function is being called by something that is in a document ready function bracket.

//ajaxfunction outside of doc ready function

$(document).ready(function () {
//ajaxfunctioncall here
});

Upvotes: 1

asymptoticFault
asymptoticFault

Reputation: 4529

Try Json.parse(data) to convert the response data into a proper javascript object literal.

Upvotes: 1

Related Questions