2012-01-17 61 views
0

我有这个在我的模型,内容是国家的完整列表:设置DropDownListFor

public IList<LookupCountry> LookupCountry { get; set; }; 
public int SelectedCountry { get; set; } 

的这个样子的

public class LookupCountry : ILookup 
{ 
    public virtual int Id { get; set; } 
    public virtual int Code { get; set; } 
    public virtual string FR { get; set; } 
} 

public interface ILookup 
{ 
    int Id { get; set; } 
    int Code { get; set; } 
    string FR { get; set; } 
} 

在视图中,我想展示该国列表和选定的值。

@Html.DropDownListFor(c => c.LookupCountry.Id, 
    new SelectList(Model.LookupCountry, 
    "Id", 
    "Value", 
    Model.SelectedCountry), 
    "-- Select Country --") 

当我这样做,我有恩的错误,在c => c.LookupCountry.Id编号是不是在视图中可用。

有什么想法?

感谢,

回答

0

更改c.LookupCountry.Idc.SelectedCountry应该做的伎俩。由于LookupCountry是一个国家集合,因此它没有Id属性。并且您希望将选定的值从下拉列表中绑定到SelectedCountry属性。

@Html.DropDownListFor(c => c.SelectedCountry, 
    new SelectList(Model.LookupCountry, 
    "Id", 
    "Value", 
    Model.SelectedCountry), 
    "-- Select Country --") 
0

那是因为LookupCountry在你的模型具有IList<LookupCountry>类型。整个清单中不包含id,其所有成员逐个包含id。可能你想重写你的方法如下

@Html.DropDownListFor(c => c.SelectedCountry, 
    new SelectList(Model.LookupCountry, 
    "Id", 
    "Value", 
    Model.SelectedCountry), 
    "-- Select Country --")