我想知道即使该字段不为空,必需属性也会在 Edit 中自动触发的可能原因是什么?
这是我的代码:
查看模型
public class ExpensesViewModel
{
public int Id { get; set; }
[Required]
[StringLength(4, MinimumLength = 4)]
[Remote(action: "IsExpenseCodeValid", controller: "Expenses", AdditionalFields = "Id",
ErrorMessage = "Expense Code already taken, please try different")]
public string Expense_Code { get; set; }
[Required]
public string Expense_Name { get; set; }
public string Category { get; set; }
public string Updated_By { get; set; }
}
编辑.cshtml代码:
@*@model QnE_Accounting.Models.MasterModels.Expenses*@
@model QnE_Accounting.Models.MasterViewModels.ExpensesViewModel
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<h4>Expenses</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="Id" />
<div class="form-group">
<label asp-for="Expense_Code" class="control-label"></label>
<input asp-for="Expense_Code" class="form-control" />
<span asp-validation-for="Expense_Code" class="text-danger"></span>
</div>
etc..
并从控制器编辑:
// GET: Expenses/Edit/5
public async Task<IActionResult> Edit(int? id, ExpensesViewModel vmodel)
{
if (id == null)
{
return NotFound();
}
var expenses = await _context.Expenses.SingleOrDefaultAsync(m => m.Id == id);
if (expenses == null)
{
return NotFound();
}
else
{
vmodel.Id = expenses.Id;
vmodel.Expense_Code = expenses.Expense_Code;
vmodel.Expense_Name = expenses.Expense_Name;
vmodel.Category = expenses.Category;
}
return View(vmodel);
}
更新,示例屏幕截图:
示例屏幕截图
我怎样才能解决这个问题?我错过了什么吗?
有只小跳蛙
陪伴而非守候
相关分类