Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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 3 接收到试图删除x和x之间的关系,但其中一个关系';s外键_Asp.net Mvc 3_Linq To Sql_One To Many - Fatal编程技术网

Asp.net mvc 3 接收到试图删除x和x之间的关系,但其中一个关系';s外键

Asp.net mvc 3 接收到试图删除x和x之间的关系,但其中一个关系';s外键,asp.net-mvc-3,linq-to-sql,one-to-many,Asp.net Mvc 3,Linq To Sql,One To Many,我有一个MVC项目,我有一个案例,我需要同时更新一个父实体和多个子实体。在后操作中,我收到“试图删除x和x之间的关系,但关系的外键之一”,这很奇怪,我所做的只是更新,我没有删除任何实体。我正在使用LINQtoSQL和MVC3。伪代码如下所示: @model Project.Models.ParentModel ... @using (Html.BeginForm()) { @Html.Label("Parent property") @Html.EditorFor(model => m

我有一个MVC项目,我有一个案例,我需要同时更新一个父实体和多个子实体。在后操作中,我收到“试图删除x和x之间的关系,但关系的外键之一”,这很奇怪,我所做的只是更新,我没有删除任何实体。我正在使用LINQtoSQL和MVC3。伪代码如下所示:

@model Project.Models.ParentModel

...

@using (Html.BeginForm()) {
@Html.Label("Parent property")
@Html.EditorFor(model => model.ParentProperty)

@foreach (var child in Model.Childs)
{
Html.RenderPartial("_EditChild", child)
// Which is nothing more than a label and an editor for a property like:
// @model Project.Models.ChildModel
// @Html.Label("Child property")
// @Hteml.EditorFor(model => model.ChildProperty)
}

...

}
该操作看起来像:

public ActionResult Edit(int id, FormCollection collection)
{

var parent = new Parent();
TryUpdateModel(Parent()); // which updates the parent and the child properties correctly
dataContext.SubmitChanges();

}

有人能解释这种行为吗。再一次,我不会删除或删除任何子实体

列表上的绑定可能非常糟糕,我自己也有一些问题。我修改了我的列表编辑代码以与childs一起使用,并对其进行了测试,它工作正常,数据在post操作中正确绑定并可见:

@model MvcApplication2.Models.Parent

@using (Html.BeginForm())
{
    <table>
        @{
                <tr>
                    <td>
                        @Html.TextBoxFor(m => m.Text)
                        @Html.HiddenFor(m => m.ID)
                    </td>
                </tr>
                for (int i = 0; i < Model.Children.Count; i++)
                {
                            <tr>
                                <td>
                                    @Html.TextBoxFor(x => x.Children[i].Title)
                                    @Html.HiddenFor(x => x.Children[i].ID)
                                </td>
                            </tr>
                }
        }
    </table>
    <div class="button">
        <input class="submit" type="submit" name="btnSave" id="btnSave" value="Save" />
    </div>
}
@model mvcapapplication2.Models.Parent
@使用(Html.BeginForm())
{
@{
@Html.TextBoxFor(m=>m.Text)
@Html.HiddenFor(m=>m.ID)
for(int i=0;ix.Children[i].Title)
@Html.HiddenFor(x=>x.Children[i].ID)
}
}
}
用于我的测试的控制器如下所示:

[HttpGet]
public ActionResult EditingChildren()
{
    Parent parent = new Parent() { Text = "" };
    parent.Children = new List<Child>();
    parent.Children.Add(new Child() { Title = "" });
    parent.Children.Add(new Child() { Title = "" });
    parent.Children.Add(new Child() { Title = "" });
    return View(parent);
}

[HttpPost]
public ActionResult EditingChildren(Parent parent)
{
    // save parent with children
}
[HttpGet]
公共行动结果编辑儿童()
{
父项=新父项(){Text=”“};
parent.Children=新列表();
parent.Children.Add(new Child(){Title=”“});
parent.Children.Add(new Child(){Title=”“});
parent.Children.Add(new Child(){Title=”“});
返回视图(父级);
}
[HttpPost]
公共行动结果编辑子项(父项)
{
//保存带子对象的父对象
}
编辑我关于使用linq将数据保存回sql的帖子:

如果未在视图上绑定ID,则post方法中的对象中该ID将保留为空。这会给您带来保存数据的麻烦。 因此,我通常将ID绑定到一个隐藏字段,使其不再为空(查看上面编辑的代码,并在TextBoxFor下面添加HiddenFor)

还可以在此网站上查看如何使用linq到sql更新数据:

(在“附加实体”、“更改属性”、“更新”下)

这篇文章:

我在局部视图中编辑时遇到一些问题。你能在没有部分视图的情况下尝试(编辑器直接在foreach中而不是部分渲染)吗?我已经用模板编辑器尝试过了,但是同样的错误!我也尝试过这个解决方案,但是模型绑定器不能绑定子实体。你能告诉我如何在POST操作中处理更新吗?我大部分时间都使用TryUpdateModel()方法,但在这种情况下,它会使子ID为0。要保存对象,必须对父linq to sql对象使用Attach()方法,然后提交更改()。这不起作用,因为数据库中已经存在实体!我通常将视图上的ID属性绑定到一个隐藏字段中,因为它将被发回。我已经编辑了我的帖子,希望能有所帮助。是的,我就是这么做的,但运气不好。奇怪的是,即使它没有正确地更新EntitySet,它也会修改列表。所以我不得不用一种丑陋的方式,在列表中迭代并从DB中获取行来更新属性。