如何在不使用JavaScript定义类的情况下创建类?

时间:2022-08-26 21:31:20

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 :

我有一个Ajax方法

[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.

我想在客户端调用这个方法,但是如何在客户端实例化Address类呢?我不想在JavaScript文件中定义一个名为Address的类。

<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?

有没有办法做到这一点?还是我需要用JavaScript写地址类?

3 个解决方案

#1


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>

#2


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.)

您必须首先序列化对象。实现这一点的一种方法是使用JSON (Javascript对象表示法)——这里有一个关于如何工作的教程。编辑:(实际上,该教程相当令人困惑,而且有点过时了——试试这个更直接、最新的方法。)

#3


0  

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

使用。net AJAX扩展,您可以添加上面的generatescript ttype标记,例如,您的服务器端ValidateAddress()

[GeneratScriptType(typeof(Address)]

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

服务器将把必要的js发送到客户端,以便您可以进行js调用

var address = new nameofyournamespace.Address()

See the msdn GenerateScriptTypeAttribute reference

请参阅msdn generatescript ttypeattribute引用

#1


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>

#2


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.)

您必须首先序列化对象。实现这一点的一种方法是使用JSON (Javascript对象表示法)——这里有一个关于如何工作的教程。编辑:(实际上,该教程相当令人困惑,而且有点过时了——试试这个更直接、最新的方法。)

#3


0  

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

使用。net AJAX扩展,您可以添加上面的generatescript ttype标记,例如,您的服务器端ValidateAddress()

[GeneratScriptType(typeof(Address)]

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

服务器将把必要的js发送到客户端,以便您可以进行js调用

var address = new nameofyournamespace.Address()

See the msdn GenerateScriptTypeAttribute reference

请参阅msdn generatescript ttypeattribute引用