【发布时间】:2019-07-04 09:35:05
【问题描述】:
我的网络应用程序布局中有一个form 中的select,需要从每个页面访问。该表单设置了在每个页面上加载数据所需的会话变量。
<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>
</select>
</form>
我知道我可以创建一个基础 PageModel 并在每个页面上继承该基础以响应 OnPost 例如
public abstract class BaseSecurePageModel : PageModel
{
[BindProperty]
public string Customer { get; set; }
public virtual void OnPostCustomerChange()
{
HttpContext.Session.SetString("Customer", Customer);
}
}
但这并不适合将模型绑定到表单,并且还要求我记得从每个页面中的基类继承。是否有正确的方法来处理需要随处可用的表单?
【问题讨论】:
-
您应该使用 ViewComponent,如本答案所述:stackoverflow.com/questions/53104614/…
标签: c# asp.net-core-2.0 razor-pages