2

I have a class at server-side like this:

public class Address
{
    public Address(string countryCode)
    {
        this._CountryCode = countryCode;
    }

    private string _CountryCode = string.Empty;
    public string CountryCode
    {
        get { return _CountryCode; }
        set { _CountryCode = value; }
    }

}

and I have a method that called by Ajax methods :

[AjaxMethod]
public static Address ValidateAddress(Address address)
{
   // Some logic here...
   return address;
}

And I want to call this method in client side, but how can I instantiate the Address class in client side? I do not want to define a class named Address in JavaScript file.

<script>

function valaddress(){ 

  var address = new Address(); // I'm asking here ??
  address.CountryCode = 90;

  // This part is ok, no problem in here : 
  var validatedAddress = AddressControlDataHelper.ValidateAddress(address).value; 

}

</script>

Is there a way to do this? Or do I need to write my Address class in JavaScript?

3 Answers 3

5
var address = { 
    CountryCode: "1"
}

That should do the trick

<script>
  function valaddress(){ 

      var address = { 
          CountryCode: "90"
      };

      // This part is ok, no problem in here : 
      var validatedAddress = 
       AddressControlDataHelper.ValidateAddress(address).value; 

  }
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

You are going to have to serialize the object first. One way to do this is with JSON (Javascript Object Notation) - here is a tutorial on how that works. EDIT: (Actually that tutorial is quite confusing and a bit outdated - try this one for a more direct, up-to-date approach.)

Comments

0

Using the .net AJAX extensions, you can add the GenerateScriptType tag above, for example, your server side ValidateAddress()

[GeneratScriptType(typeof(Address)]

The server would send the necessary js down to the client so you could make the js call

var address = new nameofyournamespace.Address()

See the msdn GenerateScriptTypeAttribute reference

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.