0

I'm building a .NET MVC project, and have a situation where the Create form for a model is not accepting user input, and still submitting 0 even when I enter a different value. As you can see below, the field can't accept the value 0, so this invalidates the model.

Relevant model field:

public class Inventory
    {
        [Key]
        [Display(Name = "Inventory ID")]
        public int InventoryID { get; set; }
        [Required]
        [Range(1, int.MaxValue, ErrorMessage = "Please enter a value larger than 0")]
        public int QTY { get; set; }

GET and POST Controller:

// GET: Inventories/Create
        public IActionResult Create()
        {
            ViewData["BinID"] = new SelectList(_context.Bins, "BinID", "BinName");
            ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "ProductDescription");
            return View();
        }

        // POST: Inventories/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("InventoryID,Quantity,BinID,ProductID")] Inventory inventory)
        {
            if (ModelState.IsValid)
            {
                _context.Add(inventory);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException ex)
                {
                    // send message to view
                    ModelState.AddModelError(string.Empty, "An inventory for this bin and product already exists");
                    ViewData["BinID"] = new SelectList(_context.Bins, "BinID", "BinName", inventory.BinID);
                    ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "ProductDescription", inventory.ProductID);
                    return View(inventory);
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["BinID"] = new SelectList(_context.Bins, "BinID", "BinName", inventory.BinID);
            ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "ProductDescription", inventory.ProductID);
            return View(inventory);
        }

View:

<div class="form-group">
    <label asp-for="QTY" class="control-label"></label>
    <input asp-for="QTY" class="form-control" />
    <span asp-validation-for="QTY" class="text-danger"></span>
</div>

EDIT: Changing to public int? QTY does not work - the same issue persists, only the error is now the the QTY field is required, as the default HTML value is null instead of 0. THe input field simply won't retain the value.

0

1 Answer 1

1

Your binding is not correct [Bind("InventoryID,Quantity,BinID,ProductID")] Inventory inventory. You should do something like that: [Bind("InventoryID,QTY")] Inventory inventory.

In fact, when you submit a form it will send a dictionary object then the Bind class will look if there is any matching between your Object (Inventory in this case) and the dictionary sent.

If your Inventory object has other properties you can and them to the binding.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - this worked. One follow up (I'm very new to .NET), why wouldn't I include BinID and Product ID (these are foreign key IDs) in the binding?
I did not see them in your Inventory class, you may dit not show as the whole class, so if they are you could add them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.