1

I have a web method in My Controller...

    [WebMethod]
    public IList<ThemeSelectList> GetThemesForSelectedCategory(string themeCategoryId)
    {
        IList<ThemeSelectList> themeSelectList = new List<ThemeSelectList>();
        int emailLayoutThemeCategoryId = Convert.ToInt32(themeCategoryId);
        using (var trans = session.BeginTransaction())
        {
            EmailThemeBusinessLogic emailThemeBusinessLogic = new EmailThemeBusinessLogic(session, null);
            themeSelectList = emailThemeBusinessLogic.GetThemes(emailLayoutThemeCategoryId);
            trans.Commit();
        }

        return themeSelectList;            
    }

that i am trying to call from a java-script function, that is

function GetThemesForSelectedCategory(event)
{
    event = event || window.event || e.srcElement;
    event.preventDefault();
    var selectedThemeCategoryId = $('#ddlThemeCategory option:selected').val();
    var ThemeContainerDiv = $("#ThemeContenerDiv");
    ThemeContainerDiv.html('<p><img src="../../../../Images/loading.gif"></p>');
    $.ajax
    ({
        type: "POST",
        url: "GetThemesForSelectedCategory",
        data: JSON.stringify({ "themeCategoryId": selectedThemeCategoryId }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            // function is not returning to success
            var ThemeDetails = data.d;
            for (var i = 1; i <= ThemeDetails.length; i++) {
                var row = ['<div id="' + ThemeDetails[i].ThemeId + '" class="themegroup divhighlight">\
                                <div class="themename">\
                                    ' + ThemeDetails[i].ThemeName + '\
                                </div>\
                                ' + GetColourTamplate(ThemeDetails[i].ThemeTemplateColorList) + ''].join('\n');
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            // always error method is getting called
            var somthing = "pankajDubey";
        },
        complete: function (data) 
        {
            var ThemeDetails = data.d;
            for (var i = 1; i <= ThemeDetails.length; i++) {
                var row = ['<div id="' + ThemeDetails[i].ThemeId + '" class="themegroup divhighlight">\
                                <div class="themename">\
                                    ' + ThemeDetails[i].ThemeName + '\
                                </div>\
                                ' + GetColourTamplate(ThemeDetails[i].ThemeTemplateColorList) + ''].join('\n');
            }
        }
    });
}

i am unable to understand what is going wrong. Every Thing in web method is working fine but i don't know what is missing. please help as I am new to MVC and NHibernate both...

1
  • what is the error you are getting? Commented Jun 4, 2013 at 6:55

1 Answer 1

11

I have a web method in My Controller...

In ASP.NET MVC controllers have actions, not web methods. Web methods are obsolete.

So:

public ActionResult GetThemesForSelectedCategory(string themeCategoryId)
{
    IList<ThemeSelectList> themeSelectList = new List<ThemeSelectList>();
    int emailLayoutThemeCategoryId = Convert.ToInt32(themeCategoryId);
    using (var trans = session.BeginTransaction())
    {
        EmailThemeBusinessLogic emailThemeBusinessLogic = new EmailThemeBusinessLogic(session, null);
        themeSelectList = emailThemeBusinessLogic.GetThemes(emailLayoutThemeCategoryId);
        trans.Commit();
    }

    return Json(themeSelectList);
}

and then:

$.ajax({
    type: "POST",
    url: "/SomeControllerName/GetThemesForSelectedCategory",
    data: { "themeCategoryId": selectedThemeCategoryId },
    success: function (data) {
        ...
    },
    error: function (xhr, ajaxOptions, thrownError) {
        ...
    },
    complete: function (data) {
        ...
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer.. instead of ActionResult i used JsonResult.. and That Solved My Problem

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.