I'm looking for a way to invoke Model binder inside View component. I prefer to use build-in functionality from asp.net core, but this is only available inside controllers, not View Components itself. Any way to resolve that problem?
-
1Is strange what you're proposing. On MVC pattern, all data binding should be made at the controller layer, so when you reach the View layer you only have to deal with data structures, not business classes. Explain more in depth your needings so we can understand what do you need and how could you reach it.Bardo– Bardo2016-04-14 12:05:06 +00:00Commented Apr 14, 2016 at 12:05
-
1Use view components to build reusable widgets/UI items (Ex : Your menu bar etc..) Leave model binding to a real action method. You can still have a form inside the view component view's code which will be posted to a real action method.Shyju– Shyju2016-04-14 12:09:59 +00:00Commented Apr 14, 2016 at 12:09
-
I know that it isn't exacly how it should be done by MVC but in that case we need to deal with it in this matter. We Have one entry point ( Action in Controller) and based on parameters we can determine which View Components should be invoked ( Information acquired from DB base on permission,passed parameters etc )Thomas K– Thomas K2016-04-14 12:15:37 +00:00Commented Apr 14, 2016 at 12:15
4 Answers
According to View Components documentation:
View Components don’t use model binding, and only depend on the data you provide when calling into it.
However, you could pass model as an object/parameter into your ViewComponent:
@await Component.InvokeAsync("PriorityList", MyModel)
or
@await Component.InvokeAsync("PriorityList", new { maxPriority = 2, isDone = false })
What do you want to achieve?
Comments
As far as I'm aware, this is not possible. The model binding occurs further up in the framework's lifecycle before the controller action is invoked.
I would be really interested in seeing some code as to why you need to do this to see if there are any other potential solutions that wouldn't involve model binding.
Comments
I solved this in my project by using ASP.NET Core's dependency injection to insert my singleton directly into a Razor view.
The code snippet below uses the SignalR JavaScript library to send a notification to the browser if something comes in while they're watching the screen. When the page first loads though, I want to display the notification count in a CSS "bell" image so I get the count from the "singleton" object registered in Startup.cs. I use the List<Notification> object in my back-end code to add new notifications to the single instance object as well as sending the signal to the browser. DI worked great in this case to solve the model binding need.
Startup.cs
services.AddSingleton<List<Notification>>();
NotificationComponent.cshtml
@using PWVault.Service.Notification //optional as you can specify the full namespace instead below
@inject List<Notification> Notifications //this is my registered Singleton from Startup.cs
<link rel="stylesheet" href="~/css/NotificationBadge.css" />
<div class="notificationContainer">
<div class="notification" data-toggle="modal" data-target="#notificationModal">
</div>
<label hidden="hidden" id="notificationCount">@Notifications.Count</label>
</div>
@*modal window*@
@await Html.PartialAsync("_NotificationDetailModal")
<script src="~/lib/signalr/dist/browser/signalr.js"></script>
<script src="~/js/NotificationHub.js"></script>