我正在使用实体框架6,MVC 5和最新的ASP.NET Razor。
我有以下代码:
[ForeignKey("Country")]
[Display(Name = "CountryId", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
public int CountryId { get; set; }
[Required(ErrorMessageResourceName = "Country_ValidationError", ErrorMessageResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
[Display(Name = "Country", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
public virtual Country Country { get; set; }
[ForeignKey("Currency")]
[Display(Name = "CurrencyId", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
public int CurrencyId { get; set; }
[Required(ErrorMessageResourceName = "Currency_ValidationError", ErrorMessageResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
[Display(Name = "Currency", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
public virtual Currency Currency { get; set; }不管我把外键放在哪里,表都是正确创建的;但是当我创建视图时:
<div class="form-group">
@Html.LabelFor(model => model.CountryId, "CountryId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CountryId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CountryId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CurrencyId, "CurrencyId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CurrencyId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CurrencyId, "", new { @class = "text-danger" })
</div>
</div>我知道错误:
键为“ViewData”的CountryId项为“System.Int32”类型,但必须是“IEnumerable”类型。
我错过了什么,当然,这应该工作,因为我不需要IEnumerable一对一的关系?
任何援助都非常感谢。
发布于 2014-11-18 09:12:25
The ViewData item that has the key 'CountryId' is of type 'System.Int32' but must be of type 'IEnumerable'.
[ForeignKey("Currency")]
[Display(Name = "CurrencyId", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
public int CurrencyId { get; set; }因为您在这里定义int CurrencyID,所以您将得到int类型。如果你想要整数,那么你必须声明如下
public IEnumerable<int> CurrencyId { get; set; }发布于 2014-11-18 10:00:13
当您想要下拉列表时,通常的做法是为您的视图使用另一个类(类似于ViewModel)。该类还将具有下拉列表的所有可能值。(您还可以在Model中使用它们,并将属性标记为[NotMapped])
public class YourViewModel : YourModel
{
public IEnumerable<int> CurrencyIds { get; set; }
}然后在控制器中填充CurrencyIds,然后在视图中使用:
@Html.DropDownListFor(model => model.CurrencyId, new SelectList(Model.CurrencyIds ))只有这样才会显示带有ID的下拉列表。用户可能实际上对货币名称或其他属性感兴趣。因此:
public class YourViewModel : YourModel
{
public IEnumerable<Currency> Currencies { get; set; }
}查看:
@Html.DropDownListFor(x => x.CurrencyId, new SelectList(Model.Currencies, "CurrencyId", "DisplayName", Model.CurrencyId))(注意,我直接绑定到SelectList中的@Model )
发布于 2014-11-18 12:33:50
谢谢大家的反馈。根据微软的文档,我只需要指定一个ForeignKey属性和虚拟属性。这当然是我如何实现这种一对一的关系,在过去与实体框架代码第一。我已经阅读了大量关于一对一关系的文章,甚至下载了示例代码,它们都能工作,这似乎是我的解决方案中的一些东西,但我不明白是什么。我下载了一个Contoso大学示例,他们将该关系指定为:
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }在那里可以看到
<div class="form-group">
<label class="control-label col-md-2" for="DepartmentID">Department</label>
<div class="col-md-10">
@Html.DropDownList("DepartmentID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })
</div>
</div>所有的电线都是正确的。
https://stackoverflow.com/questions/26990322
复制相似问题