Json 请求后 SubSonic 2.2 RecordBase.cs 中出现 NullReferenceException。为什么?

发布于 2024-11-11 01:01:01 字数 4375 浏览 2 评论 0原文

我有一个使用 WCF 服务的 MVC 项目。该服务返回位置集合,用于筛选文本框中的自动完成功能。

我最初用我自己的“Person”对象进行了尝试,以确保整个事情正常进行。

jQuery 调用:

    $(function () {

        $("#txtGeoLocation").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/home/FindLocations", type: "POST", dataType: "json",
                    data: { searchText: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.FullName, value: item.FullName, id: item.PersonId }
                            // return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
                    : "Nothing selected, input was " + this.value);
            }
        });

    });

控制器操作:

    [HttpPost]
    public JsonResult FindNames(string searchText, int maxResults)
    {
        PersonRepository repository = new PersonRepository();
        var result = repository.FindPeople(searchText, maxResults);
        return Json(result);

    }

人员存储库:

public class PersonRepository
{


    internal List<Person> FindPeople(string searchText, int maxResults)
    {

        List<Person> names = new List<Person>();

        BuildNameList(names);


        var result = from n in names
                     where n.FullName.Contains(searchText)
                     orderby n.FullName
                     select n;

        return result.Take(maxResults).ToList(); 
    }

    private static void BuildNameList(List<Person> names)
    {
        names.Add(new Person { PersonId = 1, FullName = "Kristina H. Chung" });
        names.Add(new Person { PersonId = 2, FullName = "Paige H. Chen" });
        names.Add(new Person { PersonId = 3, FullName = "Sherri E. Melton" });

工作得很好...所以我将 Json 调用从 /home/FindNames 替换为 /home/FindLocations 并取消注释以下行(注释掉我的 jQuery 调用中的 item.FullName 行):

return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }

Here是我的控制器操作:

    [HttpPost]
    public JsonResult FindLocations(string searchText, int maxResults)
    {
        GeoLocationRepository repository = new GeoLocationRepository();
        var result = repository.FindGeo(searchText, maxResults);

        return Json(result);    // returned a list collection of geo locations
    }

我的存储库方法:

    internal List<FeederService.GeoLocation> FindGeo(string searchText, int maxResults)
    {
        // get geolocations from the cache

        var result = from l in HttpRuntime.Cache["WebsiteGeoLocations"] as FeederService.GeoLocationCollection
                     where l.GeoDisplay.Contains(searchText)
                     orderby l.GeoDisplay
                     select l;

        return result.Take(maxResults).ToList();
    }

一切似乎都工作正常,直到操作方法结束:

return Json(result);    // returned a list collection of geo locations

实际上有正确的集合,其中包含正确数量的项目,但没有任何内容返回给我的客户端。 VS 弹出 SubSonic 文件 RecordBase.cs 并在公共字符串 TableName 的 get 调用处出现空引用异常:

    /// <summary>
    /// Name of the table in the database that contains persisted data for this type
    /// </summary>
    /// <value>The name of the table.</value>
    [XmlIgnore]
    [HiddenForDataBinding(true)]
    public string TableName
    {
        get { return BaseSchema.TableName; }  // <--- error occurs here
    }

发生这种情况是由于某些实体引用问题吗,因为我是引用 FeederService.GeoLocation 对象,我怎样才能得到它工作?

这些实体都在 DAL 项目中,通过我的 FeederService 引用和公开: GeoLocationCollection [CollectionDataContract]

[Serializable]
[CollectionDataContract]
public partial class GeoLocationCollection : ActiveList<GeoLocation, GeoLocationCollection>
{      
    public GeoLocationCollection() {}

我已将 [DataContract] 添加到 GeoLocation。

我还放置了“调试器”;在我的 jquery 代码中(将其移至函数中),但我无法调试返回的数据。我怎样才能做到这一点?

最后,仅当集合中存在匹配项时才会出现此问题。

I have an MVC project that consumes a WCF service. The service returns a collection of locations that is used to filter for autocomplete in a text box.

I initially tried this with with my own "Person" object to ensure I had the entire thing working.

jQuery call:

    $(function () {

        $("#txtGeoLocation").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/home/FindLocations", type: "POST", dataType: "json",
                    data: { searchText: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.FullName, value: item.FullName, id: item.PersonId }
                            // return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
                    : "Nothing selected, input was " + this.value);
            }
        });

    });

Controller Action:

    [HttpPost]
    public JsonResult FindNames(string searchText, int maxResults)
    {
        PersonRepository repository = new PersonRepository();
        var result = repository.FindPeople(searchText, maxResults);
        return Json(result);

    }

Person repository:

public class PersonRepository
{


    internal List<Person> FindPeople(string searchText, int maxResults)
    {

        List<Person> names = new List<Person>();

        BuildNameList(names);


        var result = from n in names
                     where n.FullName.Contains(searchText)
                     orderby n.FullName
                     select n;

        return result.Take(maxResults).ToList(); 
    }

    private static void BuildNameList(List<Person> names)
    {
        names.Add(new Person { PersonId = 1, FullName = "Kristina H. Chung" });
        names.Add(new Person { PersonId = 2, FullName = "Paige H. Chen" });
        names.Add(new Person { PersonId = 3, FullName = "Sherri E. Melton" });

Worked great... so I replaced my Json call from /home/FindNames to /home/FindLocations and uncommented the following line (commenting out the item.FullName line in my jQuery call):

return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }

Here is my controller action:

    [HttpPost]
    public JsonResult FindLocations(string searchText, int maxResults)
    {
        GeoLocationRepository repository = new GeoLocationRepository();
        var result = repository.FindGeo(searchText, maxResults);

        return Json(result);    // returned a list collection of geo locations
    }

My repository method:

    internal List<FeederService.GeoLocation> FindGeo(string searchText, int maxResults)
    {
        // get geolocations from the cache

        var result = from l in HttpRuntime.Cache["WebsiteGeoLocations"] as FeederService.GeoLocationCollection
                     where l.GeoDisplay.Contains(searchText)
                     orderby l.GeoDisplay
                     select l;

        return result.Take(maxResults).ToList();
    }

Everything seems to work just fine until the end of the action method:

return Json(result);    // returned a list collection of geo locations

There is in fact the right collection with the right number of items in it, but nothing returned to my client. VS pops up with the SubSonic file RecordBase.cs and a null reference exception at the get call of public string TableName:

    /// <summary>
    /// Name of the table in the database that contains persisted data for this type
    /// </summary>
    /// <value>The name of the table.</value>
    [XmlIgnore]
    [HiddenForDataBinding(true)]
    public string TableName
    {
        get { return BaseSchema.TableName; }  // <--- error occurs here
    }

Is this happening due to some entity reference issue, as I am the referencing FeederService.GeoLocation object and how can I get this to work?

The entities are all in a DAL project, referenced from and exposed through my FeederService:
GeoLocationCollection [CollectionDataContract]

[Serializable]
[CollectionDataContract]
public partial class GeoLocationCollection : ActiveList<GeoLocation, GeoLocationCollection>
{      
    public GeoLocationCollection() {}

And I've added [DataContract] to GeoLocation.

I have also placed "debugger;" in my jquery code (moved it into the function), but I am unable to debug the data returned. How can I accomplish this?

Lastly, the issue only occurs when there is a match in the collection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

束缚m 2024-11-18 01:01:01

我已经设法解决了这个问题,尽管没有我想要的那么优雅,而且我仍然想知道为什么我在引用通过我的服务公开的类时遇到问题。

对于解决方案,我创建了一个 WebsiteGeoLocation 模型类,它具有 FeederService.GeoLocation 类的一些属性。

然后,我迭代该集合,并将这些值复制到我的新本地对象中。效果很好。我将把复制功能移到缓存方法中,因此返回的对象始终是本地类型。

    List<WebsiteGeoLocation> geoList = null;

    internal List<WebsiteGeoLocation> FindGeo(string searchText, int maxResults)
    {
        // get geolocations from the cache

        var result = from l in HttpRuntime.Cache["WebsiteGeoLocations"] as FeederService.GeoLocationCollection
                     where l.GeoDisplay.Contains(searchText)
                     orderby l.GeoDisplay
                     select l;

        geoList = new List<WebsiteGeoLocation>();

        foreach (FeederService.GeoLocation location in result)
        {
            geoList.Add(new WebsiteGeoLocation
            {
                GeoID = location.GeoID,
                GeoDisplay = location.GeoDisplay,
                GeoCity = location.GeoCity,
                GeoState = location.GeoState,
                WebsiteID = location.WebsiteID,
                WorldCitiesID = location.WorldCitiesID
            });
        }

        return geoList.Take(maxResults).ToList();
    }

I've managed to solve the problem, although not as elegant as I wanted it to be, and I would still like to know why I was having issues referencing a class exposed through my service.

For the solution, I created a WebsiteGeoLocation model class that has some of the properties of the FeederService.GeoLocation class.

I then iterated through the collection, and copied the values into my new local object. Works just fine. I will be moving the copy functionality into the caching method, so the object returned is always of the local type.

    List<WebsiteGeoLocation> geoList = null;

    internal List<WebsiteGeoLocation> FindGeo(string searchText, int maxResults)
    {
        // get geolocations from the cache

        var result = from l in HttpRuntime.Cache["WebsiteGeoLocations"] as FeederService.GeoLocationCollection
                     where l.GeoDisplay.Contains(searchText)
                     orderby l.GeoDisplay
                     select l;

        geoList = new List<WebsiteGeoLocation>();

        foreach (FeederService.GeoLocation location in result)
        {
            geoList.Add(new WebsiteGeoLocation
            {
                GeoID = location.GeoID,
                GeoDisplay = location.GeoDisplay,
                GeoCity = location.GeoCity,
                GeoState = location.GeoState,
                WebsiteID = location.WebsiteID,
                WorldCitiesID = location.WorldCitiesID
            });
        }

        return geoList.Take(maxResults).ToList();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文