Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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
C# “错误”;发生引用完整性约束冲突";_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# “错误”;发生引用完整性约束冲突";

C# “错误”;发生引用完整性约束冲突";,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,你能帮我查一下密码吗 我有两种型号: public class Author { public int Id { get; set; } public string AuthorName { get; set; } public IEnumerable<Book> Books { get; set; } } public class Book { public int Id { get; set; } public string Title

你能帮我查一下密码吗

我有两种型号:

public class Author
{
    public int Id { get; set; }
    public string AuthorName { get; set; }

    public IEnumerable<Book> Books { get; set; }
}

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int YearPublish { get; set; }
    public int Price { get; set; }

    public int? AuthorId { get; set; } //FK
    public Author Author { get; set; } //navigation property
}
我的看法是:

<div class="form-horizontal">
    <h4>Book</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

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

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

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

    <div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price, "", new { @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>

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.Id) @LabelFor(model=>model.AuthorName,“AuthorName”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.AuthorName,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Author.AuthorName,“,new{@class=“text danger”}) @LabelFor(model=>model.Title,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Title,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”}) @LabelFor(model=>model.YearPublish,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.YearPublish,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.YearPublish,“,new{@class=“text danger”}) @LabelFor(model=>model.Price,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Price,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Price,“,new{@class=“text danger”})
当我尝试单击“保存”按钮时(即使我不做任何更改),出现了一个错误: 发生引用完整性约束冲突:关系一端的“Author.Id”属性值与另一端的“Book.authord”属性值不匹配。 dbBooks.Entry(book.State=EntityState.Modified中存在错误

如果我尝试添加
book.Author=null从逻辑上讲,我的AuthorNames开始消失


我还试图在Html.HiddenFor中添加一些内容,但可能我做了一些错误的事情,但没有任何更改。

嗯,您无法在书的编辑器视图中修改AuthorName。您需要另一个视图供作者修改

book.Author
-不能为
null
,因为它具有导航属性,并且只能类似于只读模式。所以只需删除author.authorname的editormodel

已修改图书列表:

Book bookEdit = dbBooks.Books.ToList().Where(a => a.Id == id).SingleOrDefault();

看看你需要做什么,我现在知道了。您只需要选择所有作者的列表,在视图中,AuthorId book的字段中会自动选择名称。 使用创建一个新的表单组

<div class="form-group">
    @Html.LabelFor(model => model.AuthorId, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
           @(Html.DropDownListFor(p => p.AuthorId, (SelectList)ViewBag.Authors, new { @class = "form-control" }))
        @Html.ValidationMessageFor(model => model.AuthorId, "", new { @class = "text-danger" })
    </div>
</div>
再次感谢

但是使用DropDownList不允许我编辑作者的姓名。。但这个解决方案非常好

我需要编辑所有4个字段,如名称、标题、年份和价格

我尝试制作一些类似复合模型的东西:

public class BookAuthorCompositeModel
{
    public Author author { get; set; }
    public Book book { get; set; }
}
更改我的获取方法:

// GET: Books/Edit/5       
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return HttpNotFound();
        }
        var compModel = new BookAuthorCompositeModel();
        compModel.book = dbBooks.Books.ToList().Where(a => a.Id == id).SingleOrDefault();
        compModel.author = dbBooks.Authors.ToList().Where(x => x.Id == id).SingleOrDefault();


        return View(bookEdit);
    }
它显示了我所需要的一切(我点击“编辑”,查看有关姓名、头衔、年份和价格的信息)

我的看法是:

    @model BookStorage.Models.BookAuthorCompositeModel

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Book</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.book.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.author.AuthorName, "AuthorName", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.author.AuthorName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.author.AuthorName, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.book.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.book.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.book.Title, "", new { @class = "text-danger" })
            </div>
        </div>

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

        <div class="form-group">
            @Html.LabelFor(model => model.book.Price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.book.Price, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.book.Price, "", new { @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>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
dbBooks.SaveChanges();导致错误。。 我认为使用这种ViewModel有助于编辑所有行,但它不会将数据共享到DB


也许你能帮个忙吗?

这可能不是问题,但我认为在编辑操作中,你应该查询该书的
Id
,而不是
AuthorId
Book bookEdit=dbBooks.Books.Include(a=>a.Author).Where(a=>a.Id==Id.Single()谢谢!但是仍然不知道如何实现作者姓名(来自模型作者)的编辑以及图书模型的标题、年份等。。。
// GET: Books/Edit/5       
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return HttpNotFound();
        }
        var compModel = new BookAuthorCompositeModel();
        compModel.book = dbBooks.Books.ToList().Where(a => a.Id == id).SingleOrDefault();
        compModel.author = dbBooks.Authors.ToList().Where(x => x.Id == id).SingleOrDefault();


        return View(bookEdit);
    }
    @model BookStorage.Models.BookAuthorCompositeModel

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Book</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.book.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.author.AuthorName, "AuthorName", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.author.AuthorName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.author.AuthorName, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.book.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.book.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.book.Title, "", new { @class = "text-danger" })
            </div>
        </div>

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

        <div class="form-group">
            @Html.LabelFor(model => model.book.Price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.book.Price, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.book.Price, "", new { @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>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
[HttpPost]
    public ActionResult Edit(Book book)
    {
        if (ModelState.IsValid)
        {
            dbBooks.Entry(BAmodel.book).State = EntityState.Modified;
            dbBooks.Entry(BAmodel.author).State = EntityState.Modified;

            dbBooks.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(book);
    }