1

我的控制器中有一个网络方法...

    [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;            
    }

我正在尝试从 java 脚本函数调用,即

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');
            }
        }
    });
}

我无法理解出了什么问题。Web 方法中的每一件事都运行良好,但我不知道缺少什么。请帮忙,因为我是 MVC 和 NHibernate 的新手...

4

1 回答 1

12

我的控制器中有一个网络方法...

在 ASP.NET MVC 控制器中有操作,而不是 Web 方法。Web 方法已过时。

所以:

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);
}

接着:

$.ajax({
    type: "POST",
    url: "/SomeControllerName/GetThemesForSelectedCategory",
    data: { "themeCategoryId": selectedThemeCategoryId },
    success: function (data) {
        ...
    },
    error: function (xhr, ajaxOptions, thrownError) {
        ...
    },
    complete: function (data) {
        ...
    }
});
于 2013-06-04T06:59:51.037 回答