(VB.NET MVC)传递到字典中的模型项是' System.Data.Entity.DynamicProxies类型

时间:2015-12-04 15:23:28

标签: asp.net-mvc vb.net

我不断得到他的错误:

  

传递到字典中的模型项是类型的   ' System.Data.Entity.DynamicProxies.Project_AEA0128FB0789822CECB09AA20866E2B89F29798BE9DFF48A921561C8509DA33&#39 ;,   但是这个字典需要一个类型的模型项   ' webapp.ProjectModel'

这是我的获取查看代码:

' GET: Projects/Edit/5
Function Edit(ByVal id As Integer?) As ActionResult
    If IsNothing(id) Then
        Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
    End If
    Dim project As Project = db.Projects.Include(Function(i) i.Info).Include(Function(p) p.Files).Where(Function(w) w.Id = id).First()
    If IsNothing(project) Then
        Return HttpNotFound()
    End If
    Return View(project)
End Function

而且,这是我的观点模型:

Public Class ProjectModel
    Public Property Id As Integer

    <Required>
    <Display(Name:="Project Naam")>
    <MaxLength(250)>
    Public Property Naam As String

    <Required>
    <DisplayFormat(NullDisplayText:="NotSpecified", DataFormatString:="{0:dd-MM-yyyy}")>
    <Display(Name:="Begin Datum")>
    Public Property BeginDate As Date

    <Display(Name:="Eind Datum")>
    Public Property EndDate As Date?

    <Required()>
    <Display(Name:="Project Manager")>
    Public Property ProjectManagerId As String

    <Required()>
    <Display(Name:="Google Map Link")>
    Public Property GoogleMapLink As String

    <Required>
    <Display(Name:="Address")>
    <MaxLength(250)>
    Public Property Address As String

    <Required>
    <Display(Name:="Description")>
    <MaxLength(250)>
    Public Property Description As String           
End Class

而且,这就是观点本身:

@ModelType Webapp.ProjectModel
@Code
    ViewData("Title") = "Edit2"
End Code

<h2>Edit2</h2>

@Using (Html.BeginForm())
    @Html.AntiForgeryToken()

    @<div class="form-horizontal">
        <h4>ProjectModel</h4>
        <hr />
        @Html.ValidationSummary(True, "", New With { .class = "text-danger" })
        @Html.HiddenFor(Function(model) model.Id)

        <div class="form-group">
            @Html.LabelFor(Function(model) model.Naam, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.Naam, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.Naam, "", New With { .class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(Function(model) model.BeginDate, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.BeginDate, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.BeginDate, "", New With { .class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(Function(model) model.EndDate, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.EndDate, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.EndDate, "", New With { .class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(Function(model) model.ProjectManagerId, "ProjectManagerId", htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ProjectManagerId", Nothing, htmlAttributes:= New With { .class = "form-control" })
                @Html.ValidationMessageFor(Function(model) model.ProjectManagerId, "", New With { .class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(Function(model) model.GoogleMapLink, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.GoogleMapLink, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.GoogleMapLink, "", New With { .class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(Function(model) model.Address, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.Address, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.Address, "", New With { .class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(Function(model) model.Description, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.Description, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.Description, "", New With { .class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
End Using

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

我不知道这个错误来自何处或如何修复它。

任何想法或修正都将不胜感激。

问候

格伦

1 个答案:

答案 0 :(得分:1)

您的视图需要Webapp.ProjectModel类型的模型,但您直接传递Project模型。您需要在编辑操作中将Project转换为ProjectModel,然后再将其发送到您的视图。