I have a Razor Page where I include one view component which is basically a simple <select>
. The intention is to use this component for reusing the logic behind a data filter.
The Default.cshtml
file is like here below, the scripts tag helper for on-content-loaded
is from here.
@model SomeNamespace.UserNetworksSelectorModel
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
<form method="get">
<select asp-for="@Model.NetworkId" asp-items="@Model.NetworkItems" class="form-control user-network-selector"></select>
</form>
<script type="text/javascript" on-content-loaded="true">
$(function () {
$('.user-network-selector').change(function () {
this.form.submit();
});
})
</script>
The model has the properties public IList<SelectListItem> NetworkItems { get; set; }
and public int NetworkId { get; set; }
(not included here for simplicity).
In the page I include the view component using the tag helper <vc:user-networks-selector></vc:user-networks-selector>
, similar to @await Component.InvokeAsync(typeof(SomeNamespace.ViewComponents.UserNetworksSelector))
.
My question is: how can I access, from methods in the PageModel
, the status in the view component? I'm interested in accessing the values of properties NetworkItems
and NetworkId
of its model.
Is it a bad practice to use view components like this?