-2

モデルに匿名型を割り当てる方法は? ViewBag を使用すると、次のように簡単に割り当てることができます。

ViewBag.certType = comboType.ToList();

システムからすべての ViewBags を削除していますが、今はそのようにしようとしています:

model.storeLocations = comboType.ToList();

次のエラーが表示されます。

 Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' 
to 'int'    S:\Projects\tgpwebged\tgpwebged\Controllers\AdminController.cs  
376 40  tgpwebged

モデル:

public class TipoDocumentoModel
    {
        public sistema_DocType Type { get; set; }
        public IEnumerable<string> Indices { get; set; }
        public IEnumerable<string> NonAssoIndices { get; set; }
        public int storeLocations { get; set; }
    }

コントローラ:

public ActionResult AdminSettingAddTipo()
    {
        SettingsModels.TipoDocumentoModel model = new SettingsModels.TipoDocumentoModel();

        //Pega os indices e locais de armazenamentos cadastrados no sistema
        using (tgpwebgedEntities context = new tgpwebgedEntities())
        {
            var obj = from u in context.sistema_Indexes select u.idName;
            model.Indices = obj.ToList();

            var comboType = from c in context.sistema_Armazenamento
                            select new
                            {
                                id = c.id,
                                local = c.caminhoRepositorio
                            };

            model.storeLocations = comboType.ToList();
        }

        return PartialView(model);
    }
4

1 に答える 1

0

List<>最初の問題は、アイテムをintプロパティに割り当てようとしていることです。

匿名射影から名前付きクラスを抽出する簡単な方法。

//model
public List<MyClass> storeLocations { get; set; }

//snip
var comboType = from c in context.sistema_Armazenamento
                select new MyClass
                        {
                            id = c.id,
                            local = c.caminhoRepositorio
                        };

storeLocations = comboType.ToList();

その他のオプション

  1. それでも動的な動作が必要な場合は、プロパティを次のように変更できますdynamic
  2. へのTuple<int, string>()投影 (2 番目のタイプを推測)
  3. 最終結果がドロップダウン リストの場合は、SelectList()
于 2012-12-06T19:18:15.187 に答える