C# ASP.NET CORE 2.2中编辑字段中的问题
当用户尝试编辑模型中声明的以下字段时,我遇到问题:C# ASP.NET CORE 2.2中编辑字段中的问题,c#,asp.net-core,razor,C#,Asp.net Core,Razor,当用户尝试编辑模型中声明的以下字段时,我遇到问题: public DateTime? Fed { get; set; } public string Feip { get; set; } Feip包含一个IP地址 观点如下: @model EBB.Domain.Models.CustomersEditViewModel @{ ViewData["Title"] = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } <
public DateTime? Fed { get; set; }
public string Feip { get; set; }
Feip包含一个IP地址
观点如下:
@model EBB.Domain.Models.CustomersEditViewModel
@{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Edit</h1>
<h4>Customers</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="item.CustomerId" />
<div class="form-group">
<label asp-for="item.Fed" class="control-label"></label>
<input asp-for="item.Fed" class="form-control" />
<span asp-validation-for="item.Fed" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="item.Feip" class="control-label"></label>
<input asp-for="item.Feip" class="form-control" />
<span asp-validation-for="item.Feip" class="text-danger"></span>
</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");}
}
// GET: Customers/Edit/5
public async Task<IActionResult> Edit(decimal? id)
{
if (id == null)
{
return NotFound();
}
var customers = await _context.Customers.FindAsync(id);
if (customers == null)
{
return NotFound();
}
CustomersEditViewModel model = new CustomersEditViewModel();
model.item = customers;
return View(model);
}
// POST: Customers/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(decimal id,CustomersEditViewModel editViewModel)
{
if (id != editViewModel.item.CustomerId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(editViewModel.item);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CustomersExists(editViewModel.item.CustomerId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
CustomersEditViewModel model = new CustomersEditViewModel();
model.item = editViewModel.item;
return View(model);
}
@model EBB.Domain.Models.CustomerEditViewModel
@{
ViewData[“标题”]=“编辑”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
编辑
客户
返回列表
@节脚本{
@{wait Html.RenderPartialAsync(“_validationScript”);}
}
控制器如下所示:
@model EBB.Domain.Models.CustomersEditViewModel
@{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Edit</h1>
<h4>Customers</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="item.CustomerId" />
<div class="form-group">
<label asp-for="item.Fed" class="control-label"></label>
<input asp-for="item.Fed" class="form-control" />
<span asp-validation-for="item.Fed" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="item.Feip" class="control-label"></label>
<input asp-for="item.Feip" class="form-control" />
<span asp-validation-for="item.Feip" class="text-danger"></span>
</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");}
}
// GET: Customers/Edit/5
public async Task<IActionResult> Edit(decimal? id)
{
if (id == null)
{
return NotFound();
}
var customers = await _context.Customers.FindAsync(id);
if (customers == null)
{
return NotFound();
}
CustomersEditViewModel model = new CustomersEditViewModel();
model.item = customers;
return View(model);
}
// POST: Customers/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(decimal id,CustomersEditViewModel editViewModel)
{
if (id != editViewModel.item.CustomerId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(editViewModel.item);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CustomersExists(editViewModel.item.CustomerId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
CustomersEditViewModel model = new CustomersEditViewModel();
model.item = editViewModel.item;
return View(model);
}
//获取:客户/Edit/5
公共异步任务编辑(十进制?id)
{
if(id==null)
{
返回NotFound();
}
var customers=wait_context.customers.FindAsync(id);
如果(客户==null)
{
返回NotFound();
}
CustomerEditViewModel=新CustomerEditViewModel();
model.item=客户;
返回视图(模型);
}
//职位:客户/编辑/5
//若要防止套印攻击,请启用要绑定到的特定属性,例如
//更多详细信息请参见http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务编辑(十进制id,CustomerEditViewModel editViewModel)
{
if(id!=editViewModel.item.CustomerId)
{
返回NotFound();
}
if(ModelState.IsValid)
{
尝试
{
_更新(editViewModel.item);
wait_context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
如果(!CustomerExists(editViewModel.item.CustomerId))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回重定向到操作(名称(索引));
}
CustomerEditViewModel=新CustomerEditViewModel();
model.item=editViewModel.item;
返回视图(模型);
}
用户尝试在标准的编辑razor视图表单中进行更改,并且在更改字段和保存时,不会应用更改
有人能帮我吗
Simone当您未在表单中指定方法时,浏览器默认为表单的名称,即
GET
用户提交表单后,浏览器向/Customers/Edit/5
端点执行GET
请求。
ASP.NET MVC将此请求重定向到控制器中的public async Task Edit(decimal?id)
操作
在HTML表单中指定方法可以解决您的问题。
在Edit(decimal?id)
操作上指定[HttpGet]
属性时,框架将阻止对该操作执行POST
请求。这将有助于您防止将来出现此类问题。如果您描述问题,将更容易获得帮助。Jon Skeet写了一篇关于如何提问的精彩文章。去看看@ZdeslavVojkovic抱歉,我省略了一段post-please-post-the-view-and-controller代码尝试指定表单的方法
Scaffholding生成此代码。。。。安灼杰!非常感谢你!