h0ax
h0ax

Reputation: 49

How to pass an argument(id) from page view to page model

let me explain a problem first. I've got a page. Page is basket representation with multiple records (items).

Every item has a delete button. What I want to achieve is to delete item from that basket (actually from database). But first I need to pass item id to page-model post method.

Right now that is what i got at Page view.(cshtml, dont really sure how to call it)

<form method="post">
     <div class="form-group">
          <input type="submit" class="btn btn-danger" value="Delete" asp-page-handler="Delete"/>
     </div>
</form> 

And here is a page model.

public IActionResult OnPostDelete(int itemId)
        {
            memberData.DeleteFromBasket(itemId);
            return RedirectToPage("./Basket");
        }

How can i pass item id to the method?

Upvotes: 1

Views: 128

Answers (1)

DavidG
DavidG

Reputation: 118987

You need to add a route parameter to the button that matched the parameter name. For example:

<input type="submit" class="btn btn-danger" value="Delete" 
    asp-page-handler="Delete"
    asp-route-itemId="@model.ItemId" />

Upvotes: 1

Related Questions