- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个包含以下模型结构的表单:
TestRecord.cs
class TestRecord {
public string Id { get; set; }
public string RecordName { get; set; }
...other props
public ICollection<RtdbFiles> RtdbFiles { get; set; }
}
RtdbFile
包含以下 Prop :
class RtdbFile {
public int Id { get; set; }
public string Filename { get; set; }
...other props
}
POST
这个模型到我的 Controller 更新,我收到以下错误:
The instance of entity type 'RtdbFile' cannot be tracked because another instance with the key value '{Id: 2021}' is already being tracked
RtdbFile
被附加到上下文中。这是我的 Controller 方法的格式:
[HttpPost("UpdateMilestones")]
public async Task<IActionResult> UpdateMilestones(string testRecordId)
{
db.ChangeTracker.LazyLoadingEnabled = false;
var record = db.TestRecords
.Include(tr => tr.RtdbFiles)
.FirstOrDefault(tr => tr.TestRecordId == testRecordId);
if (await TryUpdateModelAsync(record))
{
await db.SaveChangesAsync();
}
return RedirectToAction("Milestones", new { id = testRecordId });
}
TryUpdateModelAsync()
不能用
One-to-Many
处理情况关系?何时复制
RtdbFile
被添加到上下文中?我已经
已禁用 延迟加载并急切加载
RtdbFiles
.这与
Contoso University example by Microsoft 中所做的类似但不同之处在于他们急切加载的属性是
One-to-One
关系。
@model rtdb.Models.TestRecord
@addTagHelper *, rtdb
<input type="hidden" asp-for="@Model.TestRecordId" />
<div class="form-section-text">Milestones & Tracking</div>
<!--unrelated inputs removed -->
<div class="form-group">
<vc:file record="@Model" type="@RtdbFile.AttachmentType.TPR" approvers="true"></vc:file>
</div>
RtdbFiles
被抽象出来以查看组件:
@model rtdb.Models.ViewModels.FileViewModel
@addTagHelper *, rtdb
@using HtmlHelpers.BeginCollectionItemCore
<div class="form-group attachments">
<div class="link-header">@(Model.AttachmentType.ToString("G"))</div>
<div class="row">
<div class="col-sm-12">
@if (Model.TestRecord.RtdbFiles.Count > 0)
{
foreach (var file in Model.TestRecord.RtdbFiles.Where(f => f.IsDeleted != true && f.Type == Model.AttachmentType && f.TestRecordId == Model.TestRecord.TestRecordId).ToList())
{
<div class="attachment">
@using (Html.BeginCollectionItem("RtdbFiles"))
{
<div class="form-group">
<div class="form-row">
<input asp-for="@file.Id" hidden />
<input asp-for="@file.Type" hidden />
<div class="col-sm-6">
@if (@file.Id < 1)
{
<input class="FileInput" asp-for="@file.UploadedFile" type="file" />
}
else
{
<div><span data-file-id="@file.Id"><a href='@Url.Action("Download", "RtdbFiles", new { id = file.Id })'>@file.Filename (@file.Type.ToString("G"))</a></span></div>
}
</div>
<div class="col-sm-6">
<div>
<label asp-for="@file.FileApproverPersonnel" class="col-form-label col-form-label-sm">Approvers:</label>
<input asp-for="@file.FileApproverPersonnel" class="form-control file-approver-personnel ldap-tags" />
</div>
</div>
</div>
</div>
}
</div>
}
}
<div id="@(Model.AttachmentType.ToString("G"))s"></div>
<button type="button" class="add-file btn btn-primary" data-test-type="Other" data-attachment-type="TPR" data-container="@(Model.AttachmentType.ToString("G"))s">Add @(Model.AttachmentType.ToString("G"))</button>
<small style="display: block; margin-top: 6px">File size limit: 100MB</small>
</div>
</div>
</div>
最佳答案
显而易见的是TryUpdateModelAsync
或者 ChangeTracker
string
有一些问题外键。首先,我强烈建议您将 PrimaryKey 更改为 int
因为 EF 在这种情况下会表现出一些奇怪的行为。
但是如果你坚持,我尝试了一些方法,最后达到了这个方法:
使用 AsNoTracking
阻止对象跟踪并使用 context.Update
根据 Controller 模型更新记录后
根据您的最新型号,这是我的示例,效果很好:
楷模:
public class TestRecord
{
public string Id { get; set; }
public string RecordName { get; set; }
public virtual IList<RtdbFile> RtdbFiles { get; set; }
}
public class RtdbFile
{
public int Id { get; set; }
public string TestRecordId { get; set; }
public string Filename { get; set; }
}
RtdbFiles[{i}].Id
和
RtdbFiles[{i}].Filename
您的 View 必须将名称完全相同的项目和值发送到服务器对象才能正确生效:
@model Jordan.TestRecord
@using (Html.BeginForm("UpdateMilestones", "Home", FormMethod.Post))
{
@Html.HiddenFor(p => p.Id);
@for (int i = 0; i < Model.RtdbFiles.Count; i++)
{
@Html.Hidden($"RtdbFiles[{i}].Id", Model.RtdbFiles[i].Id);
@Html.TextBox($"RtdbFiles[{i}].Filename", Model.RtdbFiles[i].Filename);
}
<button type="submit">Save</button>
}
namespace Jordan
{
[Route("")]
public class HomeController : Controller
{
private readonly AppDbContext context;
public HomeController(AppDbContext context)
{
this.context = context;
context.Database.EnsureCreated();
}
[HttpGet]
public IActionResult Index()
{
var sampleRecord = context.TestRecords
.Include(r => r.RtdbFiles)
.FirstOrDefault();
return View(sampleRecord);
}
[HttpPost]
[Route("UpdateMilestones")]
public async Task<IActionResult> UpdateMilestones(int Id)
{
context.ChangeTracker.LazyLoadingEnabled = false;
var record = context.TestRecords
.Include(tr => tr.RtdbFiles)
.AsNoTracking()
.FirstOrDefault(tr => tr.Id == Id);
if (await TryUpdateModelAsync(record))
{
context.Update(record);
await context.SaveChangesAsync();
}
return RedirectToAction("Index");
}
}
}
关于c# - 在具有导航属性的表单中使用 TryUpdateModelAsync 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61826136/
我有一个 TryUpdateModelAsync 调用,一旦失败,只会返回 false。目前还不清楚它失败的原因。我怎样才能获得更多信息? if (await TryUpdateModelAsync(
我有一个 TryUpdateModelAsync 调用,一旦失败,只会返回 false。目前还不清楚它失败的原因。我怎样才能获得更多信息? if (await TryUpdateModelAsync(
我用这样的模型绑定(bind)更新我的数据库: var adminUpdate = await _context.Admins.FindAsync(Id); if (!String.IsNullOrE
我有以下代码: if (await TryUpdateModelAsync( hostToUpdate, "Host",
所以我正在这样做 microsoft tutorial在带有 EF 6 的 ASP.NET Core 上,它刚刚通过编辑 Controller 更新了一个模型。 这段代码让我非常困惑,我想(也许希望)
假设我有一个包含以下模型结构的表单: TestRecord.cs class TestRecord { public string Id { get; set; } public st
TryUpdateModelAsync 是抽象类 PageModel 的一个内部保护方法,在我看来,这使得它不可模拟。是否有任何方法可以对涉及此方法的任何操作进行单元测试? 我在网上看到这个解决方案:
我是一名优秀的程序员,十分优秀!