用于创建对象和子对象的页面

时间:2013-07-08 21:15:11

标签: asp.net-mvc-3 entity-framework partial-views

我确信有很多人会问这类问题,但我无法弄清楚如何说出来。

我会尽力解释。我正在建立一个设备有IP地址的以太网网络。我已经设置了我的实体框架模型,以便将ip和子网存储在一个单独的表中,以确保整个系统的唯一性。

如果用户想要的IP不在下拉列表中,我希望用户能够同时创建设备及其关联的IP。

我在设备页面上删除部分IP地址页面RenderPartial,我收到此错误:

以下是问题,如何解决此错误:

  

传递到字典中的模型项的类型为PcnWeb.Models.Device,但此字典需要类型为PcnWeb.Models.IPAddress的模型项。

以下是我的模特:

IP地址型号:

namespace PcnWeb.Models
{
    public class IPAddress
    {
        public virtual ICollection<Device> Devices { get; set; }
        [Key]
        public int ipAddressRecId { get; set; }

        public Nullable<int> ipOctet1 { get; set; }
        public Nullable<int> ipOctet2 { get; set; }
        public Nullable<int> ipOctet3 { get; set; }
        public Nullable<int> ipOctet4 { get; set; }

        public Nullable<int> smOctet1 { get; set; }
        public Nullable<int> smOctet2 { get; set; }
        public Nullable<int> smOctet3 { get; set; }
        public Nullable<int> smOctet4 { get; set; }
    }
}

设备型号:

namespace PcnWeb.Models
{
    public class Device
    {
        [Key]
        public int deviceRecId { get; set; }

        public int ipAddressRecId { get; set; }

        [Required, StringLength(64)]
        [Unique]
        public string Name { get; set; }        

        [StringLength(256)]
        public string Comment { get; set; }

        public virtual IPAddress IPAddress { get; set; }
    }
}

我原本以为让关联设备创建页面包含内联ipaddress创建页面会很容易。

这是设备页面:

@model PcnWeb.Models.Device

@{
    ViewBag.Title = "Create  a Device";
}

<h2>Create</h2>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Device</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.ipAddressRecId, "IPAddress")
        </div>
        <div class="editor-field">
            @Html.DropDownList("ipAddressRecId", String.Empty)
            @Html.ValidationMessageFor(model => model.ipAddressRecId)
        </div>
        @{
            Html.RenderPartial("~/Views/IP_Address/_Create.cshtml");
        }

        <div class="editor-label">
            @Html.LabelFor(model => model.Name, "Name")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Comment, "Comment")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comment)
            @Html.ValidationMessageFor(model => model.Comment)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

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

以下是IP地址部分:

编辑:抱歉,我忘记了这个

@model PcnWeb.Models.IPAddress

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>IPAddress</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.ipOctet1, "ipOctet1")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ipOctet1)
            @Html.ValidationMessageFor(model => model.ipOctet1)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ipOctet2, "ipOctet2")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ipOctet2)
            @Html.ValidationMessageFor(model => model.ipOctet2)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ipOctet3, "ipOctet3")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ipOctet3)
            @Html.ValidationMessageFor(model => model.ipOctet3)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ipOctet4, "ipOctet4")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ipOctet4)
            @Html.ValidationMessageFor(model => model.ipOctet4)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.smOctet1, "smOctet1")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.smOctet1)
            @Html.ValidationMessageFor(model => model.smOctet1)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.smOctet2, "smOctet2")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.smOctet2)
            @Html.ValidationMessageFor(model => model.smOctet2)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.smOctet3, "smOctet3")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.smOctet3)
            @Html.ValidationMessageFor(model => model.smOctet3)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.smOctet4, "smOctet4")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.smOctet4)
            @Html.ValidationMessageFor(model => model.smOctet4)
        </div>

    </fieldset>
}

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

所以从这一切来看,它看起来很棒,验证工作在客户端。我将不得不写一些javascript来隐藏IP地址部分,除非他们从下拉列表中选择新的。

以下是问题,如何解决此错误:

  

传递到字典中的模型项的类型为PcnWeb.Models.Device,但此字典需要类型为PcnWeb.Models.IPAddress的模型项。

1 个答案:

答案 0 :(得分:0)

此错误表示部分视图中的模型类型与传递给此视图的模型类型不匹配。但是我可以看到你试图在没有模型传递的情况下渲染局部视图 你的代码示例对我有用。

因此,要解决此问题,您可以执行以下步骤。

  1. 确保视图路径正确;)。
  2. 请尝试将“发送”模型添加到您的部分视图中,例如
  3. 主视图

    @model PcnWeb.Models.Device
    //some code here
    @Html.Partial("path_to_view", Model) 
    

    局部视图。

    @model PcnWeb.Models.Device
    @Html.DropdownListFor(x=>x.ipAddressRecId, YourDlistSource) //or anything you need
    

    这适合我。

    或者,如果您需要在局部视图中编辑子模型,则可以执行此操作

     @model PcnWeb.Models.Device
        //some code here
        @Html.Partial("path_to_view", Model.IPAddress)//pass the submodel to partial view 
    

    那么你的局部视图必须是另一种类型。

        @model PcnWeb.Models.IPAddress
    
        //some code here
    

    回答评论。要解决对象引用异常,请尝试在构造函数中初始化子模型。

    public class Device
    {
    ///properties
        public Device()
        {
            IPAddress = new IPAddress();
        }
    }