Form in Layout: Razor Pages


5 days ago


31 time


I have a select in a form in the Layout of my web app that needs to be accessible from every page. The form sets a session variable that is required to load data on every page.

<form asp-page-handler="CustomerChange" method="post" class="navbar-form navbar-left">
    <select name="Customer" id="Customer" class="form-control" onchange="this.form.submit()">
        <option value="">Select Customer</option>
        <option value="Vand">Vandalay</option>
        <option value="Kram">Kramerica</option>

I know I can make a base PageModel and inherit from that on every page in order to respond to a OnPost e.g.

public abstract class BaseSecurePageModel : PageModel
    public string Customer { get; set; }
    public virtual void OnPostCustomerChange()
        HttpContext.Session.SetString("Customer", Customer);

but this doesn't lend itself to having the model binded to the form and also requires that I remember to inherit from the base class in every page. Is there a correct way to handle forms that need to be available everywhere?

1 answers


Попробуйте использовать контроллер вместо этого и ваш ChangeCustomer ActionResult указать [Маршрут ()]. Я использую контроллер для большинства моих компоновочные предметов, таких как торговые тележки, locatization и т.д ... в бритвенных страниц и работает довольно хорошо.