我创建了一个用于创建,编辑和删除数据库中BOM的应用。
当我尝试更改项目编号时,它将使数据库中的项目编号字段为空,并且如果我更改了项目的数量,则也使项目编号字段为空,但它将提交数量更改。 / p>
您的帮助将不胜感激。 我的代码如下:
这是我用于编辑的Controller代码:
// GET: RovingHeaders/Edit/5
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
var rovingHeader = await _context.RovingHeader
.Include(s => s.RovingLine)
.FirstOrDefaultAsync(m => m.RovingNo == id);
if (rovingHeader == null)
{
return NotFound();
}
return View(rovingHeader);
}
// POST: RovingHeaders/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(string id, [Bind("RovingNo,Description,ModDate")] RovingHeader rovingHeader, [Bind("RovingNo,ItemNo,Qty,RowId")] RovingLine rovingLine)
{
if (id != rovingHeader.RovingNo)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(rovingHeader);
_context.Update(rovingLine);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!RovingHeaderExists(rovingHeader.RovingNo))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(rovingHeader);
}
非常感谢您的帮助! 我的JSON文件
Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SPECContext": "Server=(localdb)\\mssqllocaldb;Database=SPECContext-0afacde4-1953-4537-8410-5d670f37debc;Trusted_Connection=True;MultipleActiveResultSets=true",
"MONTEREYContext": "Server=(localdb)\\mssqllocaldb;Database=MONTEREYContext-0afacde4-1953-4537-8410-5d670f37debc;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
我的UI文件Edit.CSHTML
@model Spec5.Models.RovingHeader
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4></h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="RovingNo" />
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ModDate" class="control-label"></label>
<input asp-for="ModDate" class="form-control" />
<span asp-validation-for="ModDate" class="text-danger"></span>
</div>
<div class="form-group">
<table class="table">
<tr>
<th></th>
<th>Item</th>
<th>QTY</th>
</tr>
<tr>
@foreach (var item in Model.RovingLine)
{
<tr>
<td>
<input type="hidden" name="Roving Number" value="@item.RovingNo" />
</td>
<td>
<input type="text" name="Part Number" value="@item.ItemNo" />
</td>
<td>
<input type="text" name="Qty" value="@item.Qty" />
</td>
<td>
<input type="hidden" name="RowId" value="@item.RowId" />
</td>
</tr>
}
</tr>
</table>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
答案 0 :(得分:1)
我知道了。问题是名字。
<td>
<input type="text" name="Part Number" value="@item.ItemNo" />
</td>
名称必须与Model相同。我将名称从name =“ Part Number”更改为name =“ ItemNo”,一切正常。
谢谢