views:

214

answers:

3

Hi evrery one!

I have this code in my project:

var UI =
{
  Layouts:
  {
    ShowLayoutSettings: function(pid, lid) {
        My.PageServices.GetPageLayout(lid, pid, UI.Layouts._onShowLayoutSettings);
    },
    _onShowLayoutSettings: function(obj) {
      alert(obj.ID);
  }
  }
}

and in my asp.net project a web service named PageServices:

namespace My
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class PageServices : System.Web.Services.WebService
    {
     public PageServices()
     {
     }
     [WebMethod]
     [ScriptMethod(UseHttpGet = false, XmlSerializeString = true)]
     [GenerateScriptType(typeof(PageLayout))]
     public PageLayout GetPageLayout(string lid, int pid)
     {
      if (!SystemModel.UserManager.HasLogin())
       return null;
      var o = SystemModel.Layouts.GetPageLayout(pid);
      o.Tag = lid;
      return o;
     }
    }
}

I should mention that my PageLayout class is a linq class and it's serialization mode is Unidirectional.

and finally a anchor link:

<a href="#" onclick="UI.Layouts.ShowLayoutSettings(5,2);">Test</a>

My problem is it is correct and sends ajax request to my service when I click this link and my service returns the object as needed, but it does not fire _onShowLayoutSettings as the call back function for this request.

I tested this work when I create a web servive which just returns and String object, and it was all correct, but I don't know why for my PageLayout object, it's not correct.

Please help me. Thank you.

+1  A: 

if it worked with returning a string, then you probably need to tell the ajax extension to create javascript code for the object you're trying to return. Add an attribute above your webmethod

[GenerateScriptType(typeof(PageLayout))]

where PageLayout is the name of the class the GetPageLayout returns

David Archer
I thought when I do this it will do correct, but it does not. I saw the HTMl source code and saw this code: var gtc = Sys.Net.WebServiceProxy._generateTypedConstructor;Type.registerNamespace('DALEntities');if (typeof(DALEntities.PageLayout) === 'undefined') {DALEntities.PageLayout=gtc("DALEntities.PageLayout");DALEntities.PageLayout.registerClass('DALEntities.PageLayout');}//]]></script>
Hossein Margani
Ok i still think putting that attribute in there is in the right direction. If its still not working, can you edit your post to add the attribute
David Archer
A long shot, but could it be that you've got the extra comma (,) after your definition of _onShowLayoutSettings(). some browsers (IE i think) won't like that. I think FF might be more forgiving
David Archer
I did correct it, I was wrong in the code, extra comma was because of it was among other functions, and in my test last function does not have this comma.
Hossein Margani
A: 

I found the solution, but it's very hard, I wrote a JavaScriptConverter named PageLayoutJavaScriptConverter like this:

public class PageLayoutJavaScriptConverter : JavaScriptConverter
{
    public override IEnumerable<Type> SupportedTypes
    {
     get
     {
      return new Type[] { typeof(PageLayout) };
     }
    }
    public override object Deserialize(
     IDictionary<string, object> dictionary,
     Type type,
     JavaScriptSerializer serializer)
    {
     throw new NotImplementedException();
    }
    public override IDictionary<string, object> Serialize(
     object obj,
     JavaScriptSerializer serializer)
    {
     PageLayout e = obj as PageLayout;
     Dictionary<string, object> result = new Dictionary<string, object>();
     if (e != null)
     {
      result.Add("ID", e.ID);
     }
     return result;
    }
}

and add this tag to web.config:

<system.web.extensions>
<scripting>
    <webServices>
     <jsonSerialization>
      <converters>
       <add name="PageLayoutJavaScriptConverter" type="WebApp.Code.PageLayoutJavaScriptConverter"/>
      </converters>
     </jsonSerialization>
    </webServices>
    <scriptResourceHandler enableCaching="true" enableCompression="true"/>
</scripting>
</system.web.extensions>

and all was correct.

I have a question, Isn't any other simpler way to do this work?

Hossein Margani
yes, I'm surprised your code before this converter doesn't work. I've had similar working before. This seems like overkill. But well done for getting something working
David Archer
yeah! thank you so much for answering.
Hossein Margani
A: 

how to call web service with jquery