0

我的视图中有以下内容:

 @Html.DropDownListFor(model => model.Search, new SelectList(Model.SearchOptions))

在我的搜索对象中,我有:

    public List<string> Search { get; set; }

    public Dictionary<string, string> SearchOptions { get; set; }

    public SearchModel GetDropDownOptions(SearchModel model)
    {
        model.SearchOptions = HelperModel.GetRefValues(db, Constants.SEARCH, false);

        return model;
    }

随着它的调用:

public static Dictionary<String, String> GetRefValues(DBEntities db, string refType, bool addEmpty)
    {

        Dictionary<String, String> res = (from c in db.References
                                          where c.Type == refType
                                          select c).ToDictionary(c => c.Key.ToString(),
                                                                 c => c.Value.ToString());

        if (addEmpty)
            res.Add("", "");

        return res;
    }

但是,我收到一条错误消息:“对象引用未设置为对象的实例。”

建议表示赞赏。

谢谢。

4

1 回答 1

2

您应该验证所有公共方法的参数。所以你的方法:

GetRefValues(DBEntities db, string refType, bool addEmpty)

应该包括检查是否db为空以及是否refType为空。如果这样做,您将能够看到这两个中的哪一个为空,然后您可以通过回溯以查看它们的初始化位置(或不初始化,视情况而定)来相应地修复您的代码。

于 2012-05-09T18:22:31.663 回答