2

我使用 MVC 3 和大规模 ORM。

我想知道如何使用 Massive ORM 填充下拉列表以从数据库中获取数据。

我使用 ViewData["Categoreis"] 将类别列表传递给我的视图。它将数据传递给视图,但是当我尝试在浏览器中加载页面时收到此错误消息:

DataBinding:“System.Dynamic.ExpandoObject”不包含名为“CategoryID”的属性。

这就是我的下拉列表的样子:

@Html.DropDownListFor(model => model.CategoryID, new SelectList(ViewData["Categories"] as IEnumerable<dynamic>, "CategoryID", "Name"), "--Category--")

有人可以解决我的问题吗?

4

3 回答 3

3

我目前正在使用 Massive。以下是我如何从数据库中的表中填充国家下拉列表:

这是在我的控制器中:

DetailsModel model = new DetailsModel();
var _countries = new Countries(); //Massive class
model.Countries = _countries.All().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name });

这是Countries我里面的财产DetailsModel

public IEnumerable<SelectListItem> Countries { get; set; }

在我看来:

@Html.LabelFor(m => m.Country)
@Html.DropDownList("Country", Model.Countries)
@Html.ValidationMessageFor(m => m.Country)

对我来说就像一个魅力。

于 2011-09-23T04:14:21.163 回答
1

我看起来为此目的有一个名为 KeyValues 的 Massive 方法。目前它是源代码中的第 360 行。它返回一个字典而不是一个 Expando。我假设您继续在代码中的其他地方使用您的 Expando。

这是方法签名:

/// This will return a string/object dictionary for dropdowns etc  
public virtual IDictionary<string, object> KeyValues(string orderBy = "") {...}
于 2011-09-28T02:30:39.883 回答
0

我使用 ViewData["Categoreis"] 将类别列表传递给我的视图

我建议您使用模型而忘记 ViewData/ViewBag。例如定义以下视图模型:

public class MyViewModel
{
    public int CategoryID { get; set; }
    public SelectList Categories { get; set; }
}

并在控制器中填充模型并传递给视图:

public ActionResult Index()
{
    var categories = _repository.GetCategories();
    var model = new MyViewModel
    {
        // this assumes that categories is an IEnumerable<T>
        // where T is some domain model having CategoryID and Name properties
        Categories = new SelectList(categories, "CategoryID", "Name")
    };
    return View(model);
}

最后在您的强类型视图中:

@model MyViewModel
@Html.DropDownListFor(x => x.CategoryID, Model.Categories, "--Category--")
于 2011-09-18T07:07:13.610 回答