为了账号安全,请及时绑定邮箱和手机立即绑定

ASP.net Core 必需属性在编辑中自动触发

ASP.net Core 必需属性在编辑中自动触发

C#
梦里花落0921 2021-11-14 10:46:49
我想知道即使该字段不为空,必需属性也会在 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);    }更新,示例屏幕截图:示例屏幕截图我怎样才能解决这个问题?我错过了什么吗?
查看完整描述

2 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

您需要创建一个 Edit 方法来处理 Post


[HttpPost]

[ValidateAntiForgeryToken]

public async Task<IActionResult> Edit(int? id, ExpensesViewModel vmodel)

{

    ... perform edit

}


查看完整回答
反对 回复 2021-11-14
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

通过删除 viewmodel 参数并在函数中声明来解决此问题,谢谢


// GET: Expenses/Edit/5

    public async Task<IActionResult> Edit(int? id)

    //public async Task<IActionResult> Edit(int? id)

    {

        if (id == null)

        {

            return NotFound();

        }


        var expenses = await _context.Expenses.SingleOrDefaultAsync(m => m.Id == id);

        //if (expenses == null)

        //{

        //    return NotFound();

        //}


        //return View(expenses);


        ExpensesViewModel vmodel = new ExpensesViewModel();


        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);

    }


查看完整回答
反对 回复 2021-11-14
  • 2 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信