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

我想知道即使该字段不为空,必需属性也会在 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);

    }

更新,示例屏幕截图:


示例屏幕截图


我怎样才能解决这个问题?我错过了什么吗?


梦里花落0921
浏览 196回答 2
2回答

有只小跳蛙

您需要创建一个 Edit 方法来处理 Post[HttpPost][ValidateAntiForgeryToken]public async Task<IActionResult> Edit(int? id, ExpensesViewModel vmodel){&nbsp; &nbsp; ... perform edit}

陪伴而非守候

通过删除 viewmodel 参数并在函数中声明来解决此问题,谢谢// GET: Expenses/Edit/5&nbsp; &nbsp; public async Task<IActionResult> Edit(int? id)&nbsp; &nbsp; //public async Task<IActionResult> Edit(int? id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (id == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return NotFound();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var expenses = await _context.Expenses.SingleOrDefaultAsync(m => m.Id == id);&nbsp; &nbsp; &nbsp; &nbsp; //if (expenses == null)&nbsp; &nbsp; &nbsp; &nbsp; //{&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; return NotFound();&nbsp; &nbsp; &nbsp; &nbsp; //}&nbsp; &nbsp; &nbsp; &nbsp; //return View(expenses);&nbsp; &nbsp; &nbsp; &nbsp; ExpensesViewModel vmodel = new ExpensesViewModel();&nbsp; &nbsp; &nbsp; &nbsp; if (expenses == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return NotFound();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vmodel.Id = expenses.Id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vmodel.Expense_Code = expenses.Expense_Code;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vmodel.Expense_Name = expenses.Expense_Name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vmodel.Category = expenses.Category;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return View(vmodel);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP