Reputation: 1056
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.
Upvotes: 0
Views: 512
Reputation: 1056
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?
Upvotes: 0
Reputation: 3060
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
Upvotes: 1