Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc EF 6,MVC模型绑定-HttpPost到控制器上缺少引用_Asp.net Mvc_Entity Framework_Razor - Fatal编程技术网

Asp.net mvc EF 6,MVC模型绑定-HttpPost到控制器上缺少引用

Asp.net mvc EF 6,MVC模型绑定-HttpPost到控制器上缺少引用,asp.net-mvc,entity-framework,razor,Asp.net Mvc,Entity Framework,Razor,我有一个包含实体引用集合的EF6模型: public class Report : Entity<int> { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ReportId { get; set; } { ... } public virtual ICollection<AllocationItem> AllocationItems

我有一个包含实体引用集合的EF6模型:

public class Report : Entity<int>
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ReportId { get; set; }

    { ... }

    public virtual ICollection<AllocationItem> AllocationItems { get; set; }
}


public class AllocationItem : Entity<int>
{
    public int ReportId { get; set; }

    [ForeignKey("ReportId")]
    public virtual Report Report { get; set; }

    { ... }
}
呈现的HTML:

@for (var i = 0; i < Model.ReportHeader.AllocationItems.Count; i++)
{
    @Html.HiddenFor(m => m.ReportHeader.AllocationItems[i].Id)
    @Html.HiddenFor(m => m.ReportHeader.AllocationItems[i].ReportId)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].AllocationTotal)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].AllocationType)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].AllocationUocAmount)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].SortOrder)
}
<input id="ReportHeader_AllocationItems_0__Id" name="ReportHeader.AllocationItems[0].Id" type="hidden" value="25">
<input id="ReportHeader_AllocationItems_0__ReportId" name="ReportHeader.AllocationItems[0].ReportId" type="hidden" value="7">
<input id="ReportHeader_AllocationItems_0__AllocationTotal" name="ReportHeader.AllocationItems[0].AllocationTotal" type="text" value="8,310,766">
<input id="ReportHeader_AllocationItems_0__AllocationType" name="ReportHeader.AllocationItems[0].AllocationType" type="number" value="3">
<input id="ReportHeader_AllocationItems_0__AllocationUocAmount" name="ReportHeader.AllocationItems[0].AllocationUocAmount" type="text" value="7624.49">
<input id="ReportHeader_AllocationItems_0__SortOrder" name="ReportHeader.AllocationItems[0].SortOrder" type="number" value="661204592">

HttpPost上的Fiddler捕获到控制器:

@for (var i = 0; i < Model.ReportHeader.AllocationItems.Count; i++)
{
    @Html.HiddenFor(m => m.ReportHeader.AllocationItems[i].Id)
    @Html.HiddenFor(m => m.ReportHeader.AllocationItems[i].ReportId)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].AllocationTotal)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].AllocationType)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].AllocationUocAmount)
    @Html.EditorFor(m => m.ReportHeader.AllocationItems[i].SortOrder)
}
<input id="ReportHeader_AllocationItems_0__Id" name="ReportHeader.AllocationItems[0].Id" type="hidden" value="25">
<input id="ReportHeader_AllocationItems_0__ReportId" name="ReportHeader.AllocationItems[0].ReportId" type="hidden" value="7">
<input id="ReportHeader_AllocationItems_0__AllocationTotal" name="ReportHeader.AllocationItems[0].AllocationTotal" type="text" value="8,310,766">
<input id="ReportHeader_AllocationItems_0__AllocationType" name="ReportHeader.AllocationItems[0].AllocationType" type="number" value="3">
<input id="ReportHeader_AllocationItems_0__AllocationUocAmount" name="ReportHeader.AllocationItems[0].AllocationUocAmount" type="text" value="7624.49">
<input id="ReportHeader_AllocationItems_0__SortOrder" name="ReportHeader.AllocationItems[0].SortOrder" type="number" value="661204592">

绑定后的控制器结果

因为这些实体引用没有被绑定,所以我有引用完整性问题。即,尝试执行保存时:

System.InvalidOperationException:'引用完整性约束 发生冲突:“Report.ReportId”的属性值位于一个 关系的结束与的属性值不匹配 另一端为'AllocationItem.ReportId'

我不需要为这个定制绑定模型,是吗?我确实看到了Fiddler显示名称属性的不同之处。例如,Fiddler显示的是
ReportHeader.AllocationItems[0.ReportId]
,而不是
ReportHeader.AllocationItems[0].ReportId的HTML名称属性中指定的内容


有人知道我做错了什么吗错了吗?IMHO在客户端和服务器之间传递实体。仅将视图所需的属性和相关数据投影到视图模型中,将其发送到客户端,然后发送一个更新的结构,其中包含可以更新回来的属性。按ID加载实体、更新值并保存。重新连接实体并设置修改后的状态意味着传递/持久化的数据远远超过需要的数量,从而导致类似这样的断开连接问题,并使您的系统暴露于篡改您不希望更改的数据的情况下。使用ViewModels。我正在使用ViewModels和EntityDto。我只是在客户端和服务器之间传递必要的数据。