gpt4 book ai didi

c# - 如何使用 Entity Framework 使用外键保存新记录?

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:25 25 4
gpt4 key购买 nike

所以我有两个模型,Product 和 Review。 Product 模型包含一个名为 Review 的导航属性,如下所示:

Public class Product
{
[HiddenInput(DisplayValue=False])
public int ProductID {get; set;}
[Required]
public string ProductName {get; set;}
[HiddenInput(DisplayValue=False)
public ICollection<Reviews> {get; set;}
}

评论模型:

public class Review
{
[HiddenInput(DisplayValue=false)]
public int ReviewId { get; set; }
[Required]
public int ProductID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Body { get; set; }
}

我有一个用于 ProductDetails 的 Action ViewResult 方法,它获取所有产品以及所有具有相同 FK ProductID 的评论,如此处所示下面:

public ViewResult ProductDetails(int productId)
{
Product product = repository.Products
.Include(p => p.Reviews)
.FirstOrDefault(p => p.ProductID == productId);
return View(product);
}

然后有一个 View,其中包含非常简单的 HTML 元素,您可以在下面看到它:

@model app.models.Product

@Html.DisplayFor(model => model.ProductName)

@foreach (var review in Model.Reviews)
{
@Html.Partial("_Reviews", review)
}

在上面的 View 中,我要求使用局部 View 来呈现属于该特定产品的所有评论。在部分 View 中,我有一个表单,用户可以在其中提交评论。我希望用户提交对名称位于 View 顶部的产品的评论。我知道我必须为此创建一个操作,但唯一让我感到不舒服的是我不知道如何获取该特定产品的 ProductID

编辑:根据斯蒂芬的回答,我不得不将表单 Html 元素放入 ProducDetails 的主视图中,并放置了一些脚本来调用 Json 结果以将数据保存在数据库中。

保存评论的 Controller 方法:

[HttpPost]
public JsonResult CreateReview (Review review)
{
if (review.ReviewId == 0)
{
db.Reviews.Add(review);
}
else
{
Review dbEntry = db.Reviews.Find(review.ReviewId);
if (dbEntry != null)
{
dbEntry.ProductID = review.ProductID;
dbEntry.Name = review.Name;
dbEntry.Subject = review.Subject;
dbEntry.Body = review.Body;
}
}
db.SaveChanges();
return Json(true);
}

最佳答案

您想要为 Product 创建一个新的 Review,因此表单需要位于主视图中,而不是部分 View 中。以下假设您要使用 ajax 提交并更新现有评论列表

@model app.models.Product
@Html.DisplayFor(model => model.ProductName)
// display existing reviews
<div id="reviews">
@foreach (var review in Model.Reviews)
{
@Html.DisplayFor(m => review.Body)
}
</div>
<h2>Create new Review</h2>
@Html.Partial("_NewReview", new Review(){ ProductID = Model.ProductID })

_NewReview.cshtml 在哪里

@model Review
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.ProductID)
@Html.EditorFor(m => m.Name)
@Html.EditorFor(m => m.Subject)
@Html.TextAreaFor(m => m.Body)
<input id="create" type="submit" value="Create" />
}

并删除_Reviews.cshtml文件

然后在主视图中,添加如下脚本

var url = '@Url.Action("Create", "Review")';
var reviews = $('#reviews');
var form = $('form');
$('#create').click(function() {
$.post(url, form.serialize(), function(response) {
if (response) {
reviews.append($('#Body').val()); // add the new Review to the existing reviews
form.get(0).reset(); // reset the form controls
}
}).fail(function (result) {
// oops
});
return false; // cancel the default submit
});

将提交给(在 ReviewController 中)

[HttpPost]
public JsonResult Create(Review model)
{
// Save the model
return Json(true); // return something to indicate success
}

关于c# - 如何使用 Entity Framework 使用外键保存新记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35498632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com