Home > OS >  The form does not send data (Product Id) to the Razor Pages action methods
The form does not send data (Product Id) to the Razor Pages action methods

Time:06-20

[enter image description here][1]

как выглядит ошибка: [1]: https://i.stack.imgur.com/6PkFJ.png The essence of the matter: there is a list of products that can be added to the cart, but when you click on Add To Cart, the form does not transmit the product Id, it transmits only zero and in the meantime such a product is not found and the link to the object is lost.

View with form:

@model PagedList<Product>
<div >
    
                @foreach (Product p in Model) {
                    <div >
                        <div >
                            <div >
                                <div >
                                    <h4>
                                        @p.Name
                                        <span 
                                              style="float:right">
                                            <small>[email protected]</small>
                                        </span>
                                    </h4>
                                </div>
                               
                                <form id="@p.Id" asp-page = "/Cart"
                                       method="post">
                                       
                                    <input type="hidden" name="Id" value="@p.Id" />
                                    <input type="hidden" name="Name"
                                           value="@p.Name" />
                                          
                                            
                                    <input type="hidden" name="RetailPrice"
                                           value="@p.RetailPrice" />
                                    <input type="hidden" name="returnUrl" value="@ViewContext.HttpContext.Request.PathAndQuery()" />
                                    <span >
                                        @(p.Description
                                            ?? "(No Description Available")
                                        <button type="submit"
                                                
                                                style="float:right">
                                            Add To Cart
                                        </button>
                                    </span>
                                </form>
                            </div>
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>
</div>

The Product Id should be passed to the onpost, but 0 is passed there

  public class CartModel : PageModel
    {
       private IRepository repository;
        public Cart Cart { get; set; }
        public CartModel(IRepository repo, Cart cartService)
        {
            repository = repo;
            Cart = cartService;
        }
        public string ReturnUrl { get; set; }
        public void OnGet(string returnUrl)
        {
         ReturnUrl = returnUrl ?? "/store";

        }
        public IActionResult OnPost(long productId, string returnUrl)
        {
            Product product = repository.Products.FirstOrDefault(p=>p.Id == productId);
            Cart.AddItem(product, 1);
            return RedirectToPage(new { returnUrl = returnUrl });

        }
        public IActionResult OnPostRemove(long productId, string returnUrl)
        {
            Cart.RemoveLine(Cart.Lines.First(c=>c.Product.Id == productId).Product);
            return RedirectToPage(new {returnUrl = returnUrl});

        }
    }
}

I don't understand why this is happening, I need your help

CodePudding user response:

the name of the input form and the name of the parameter you get on the "OnPost" method should match each other.

instead of this <input type="hidden" name="Id" value="@p.Id" />
try this <input type="hidden" name="productId" value="@p.Id" />

CodePudding user response:

In Razor Pages model binding, when you involves adding parameters to the handler method. The parameters are named after the form fields, and given an appropriate type for the expected data. When the form is posted, the Razor Pages framework calls the OnPost method and sees that it has two parameters.

public IActionResult OnPost(long productId, string returnUrl)
        {
            Product product = repository.Products.FirstOrDefault(p=>p.Id == productId);
            Cart.AddItem(product, 1);
            return RedirectToPage(new { returnUrl = returnUrl });

        }

It extracts posted form values that match the names of the parameters and automatically assigns the values from the form to the parameters if the value can be converted to the type represented by the parameter.

You use productId in OnPost method, But in your View with form, I cannot see your productId field. Please check it, and make some change .

  • Related