【问题标题】:Why binding navigational property to dropdown list is not working?为什么将导航属性绑定到下拉列表不起作用?
【发布时间】:2019-04-16 13:27:52
【问题描述】:

我在razor mvc 中填写DropDownListFor。我正在尝试从 Navigational 属性中填充它,但它不起作用。

错误:

找不到类型或命名空间名称“模型”(您是否缺少 using 指令或程序集引用?)**

  <div class="form-group">
            @Html.LabelFor(model => model.InspectionReport.InspectionPhase, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.DropDownListFor(model => model.InspectionReport.InitialsID, (IEnumerable<model.InspectionReport.Initials>) Model.InspectionReport.Initials,
                   "-Select-",
                 new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.InspectionReport.InspectionPhase, "", new { @class = "text-danger" })
            </div>
        </div>

InspectionReport.cs

namespace VAILCertificates.DAL.Entities
{
    public class InspectionReport
    {
        //General Details
        public int InspectionReportID { get; set; }
        public short? InitialsID { get; set; }
        public virtual Initials Initials { get; set; }


    }
}

姓名首字母.cs

public class Initials
{
    public short? InitialsID { get; set; }
    public string Code { get; set; }
}

【问题讨论】:

  • 显示你的控制器代码。

标签: c# asp.net-mvc entity-framework razor entity-framework-6


【解决方案1】:
(IEnumerable<model.InspectionReport.Initials>) Model.InspectionReport.Initials

很遗憾,您不能像这样推断泛型参数类型 - 您需要在此处提供固定类型或接口,而不是使用 model.something。使用泛型时,您的类型必须在编译时由编译器解析,因此您不能在那里使用任何动态代码,既不是对象的属性,也不是typeof(something)

更多关于泛型类型规范的信息可以在MSDN找到。

在您的具体情况下,您需要将这段代码替换为类似

(IEnumerable<Necessary.Namespaces.Initials>) Model.InspectionReport.Initials

,将 Necessary.Namespaces 替换为您的 Initials 类所在的任何命名空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多