0

I want to know what is the possible reason why required attributes automatically triggers in Edit even though the field is not empty?

Here is my code:

View Model

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

Edit.cshtml code:

  @*@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..

And Edit from Controller:

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

Update, sample screen shot:

Sample screen shot

How can I possibly fix this issue? Am I missing something?

Thanks

  • Hi, which field? What's the error? And what is the value? – Stefan Aug 03 '18 at 07:27
  • What do you mean _automatically triggers in Edit_? –  Aug 03 '18 at 07:28
  • @StephenMuecke, already included screen shot sir, field that has Required attribute – IAmANoobProgrammer Aug 03 '18 at 07:34
  • You need to remove the `ExpensesViewModel vmodel` parameter from your method (and initialize the model inside the method). To understand what is happening, refer [TextBoxFor displaying initial value, not the value updated from code](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Aug 03 '18 at 07:36
  • @StephenMuecke, Hi Sir already fix the problem. followed your advice..thank you – IAmANoobProgrammer Aug 03 '18 at 07:39

2 Answers2

0

Fix the issue by removing the viewmodel parameter and declare it inside the function, thanks

// 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);
    }
0

You'll want to create an Edit method to handle the Post

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int? id, ExpensesViewModel vmodel)
{
    ... perform edit
}
Mel Gerats
  • 2,234
  • 1
  • 16
  • 33