我的模型中有这样的内容,内容是国家的完整列表:
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 --")当我这样做时,出现了en错误,视图中的Id在c => c.LookupCountry.Id中不可用。
知道吗?
谢谢,
发布于 2012-01-17 12:51:08
将c.LookupCountry.Id更改为c.SelectedCountry应该能做到这一点。因为LookupCountry是国家的集合,所以它上没有Id属性。并且希望将所选值从下拉列表绑定到SelectedCountry属性。
@Html.DropDownListFor(c => c.SelectedCountry,
new SelectList(Model.LookupCountry,
"Id",
"Value",
Model.SelectedCountry),
"-- Select Country --")发布于 2012-01-17 12:51:49
这是因为模型中的LookupCountry具有IList<LookupCountry>类型。整个列表不包含id,它的所有成员一个接一个地包含id。您可能希望按照以下方式重写您的方法
@Html.DropDownListFor(c => c.SelectedCountry,
new SelectList(Model.LookupCountry,
"Id",
"Value",
Model.SelectedCountry),
"-- Select Country --")https://stackoverflow.com/questions/8894652
复制相似问题