2

MVC 3 と Massive 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 に答える