user17847724
user17847724

Reputation:

Can't show the data in form

when the user opens the form, I want the data I added to the product instance to be visible in the form but my product instance is not showing up on my form. What am I missing?

My Code :

 public IActionResult CreateProduct()
        {
            var product = new Products() {

                ProductName = "name",
                Quantity = 5
            };
            return View(product);
        }
[HttpPost]
       
public IActionResult CreateProduct(Products product)
        {
            return View();
        }

**VIEW**

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model Products

<form asp-action="CreateProduct" asp-controller="ModelB" method="post">

    @Html.TextBoxFor(a => a.ProductName, "", new { placeholder = "Product Name" })
    <br />
   
    @Html.TextBoxFor(a => a.Quantity, "", new { placeholder = "Quantity", type = "number" })
    <br />
    <button type="submit">add</button>


</form>

Upvotes: 2

Views: 49

Answers (1)

Serge
Serge

Reputation: 43890

try to rename your action to Index

[Route("~/home/index")]
 public IActionResult Index()
        {
            var product = new Products() {

                ProductName = "name",
                Quantity = 5
            };
            return View(product);
        }

Upvotes: 1

Related Questions