我敢肯定有很多人问过这类问题,但我不知道该怎么说。
我会尽力解释。我正在对设备具有 IP 地址的以太网网络进行建模。我已经设置了我的实体框架模型,以便将 ip 和子网存储在单独的表中,以确保整个系统的唯一性。
如果他们想要的 IP 不在下拉列表中,我希望用户能够同时创建设备及其关联的 IP。
我在设备页面上设置了 IP 地址页面 RenderPartial 的一部分,但出现此错误:
这是问题,如何解决此错误:
传入字典的模型项是 type
PcnWeb.Models.Device
,但是这个字典需要一个 type 的模型项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 地址。
这又是一个问题,我该如何解决这个错误:
传入字典的模型项是 type
PcnWeb.Models.Device
,但是这个字典需要一个 type 的模型项PcnWeb.Models.IPAddress
。