0

これは私を夢中にさせています!データベースから値を取得するドロップダウン リストを作成しようとしています。私はMVCが初めてなので、おそらく単純なものですが、わかりません。助けてください!

モデルを見る:

public class LoadInputModel
{
    public GeoRegion GeoRegion { get; set; }
    public System.Guid Id { get; set; }
    public IEnumerable<GeoRegion> Description { get; set; }

}

これが私のコントローラーです:

[HttpPost]
    public ActionResult LoadShape(LoadInputModel LoadInputModel)
    {
        LoadInputModel.Description = db.GeoRegions.Select(a => a);

        return View(LoadInputModel);
}

これが私のhtmlヘルパーです:

@Html.DropDownListFor(m => m.Description,Model.Description.Select(c => new SelectListItem { Text = c.Description, Value = c.Id.ToString() }), "-----Select Category----")
4

1 に答える 1

0

あなたのDescription プロパティ値null

 if( LoadInputModel.Description==null)
 {
     LoadInputModel.Description=new List<GeoRegion>();
 }
 LoadInputModel.Description = db.GeoRegions.Select(a => a);

または、クラスレベル自体で行うことができるので、多くの場所で行う必要はありません

public class LoadInputModel
{
    public GeoRegion GeoRegion { get; set; }
    public System.Guid Id { get; set; }
    public IEnumerable<GeoRegion> Description { get; set; }

    public LoadInputModel()
    {
       if(Description==null)
        {
         Description=new List<GeoRegion>();
        }
    }
}
于 2012-05-31T16:50:24.903 に答える