【问题标题】:Calling WebMethod returning IList<T> from Jquery Ajax with NHibernate And MVC使用 NHibernate 和 MVC 从 Jquery Ajax 调用返回 IList<T> 的 WebMethod
【发布时间】:2013-06-04 06:14:44
【问题描述】:

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

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

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

【问题讨论】:

  • 您遇到的错误是什么?

标签: asp.net-mvc asp.net-mvc-3 jquery nhibernate


【解决方案1】:

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

在 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) {
        ...
    }
});

【讨论】:

  • 感谢您的回答.. 我使用 JsonResult 而不是 ActionResult.. 这解决了我的问题
猜你喜欢
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多