C
C#2y ago
kunio_kun

❔ ASP.NET Core MVC Put Value to Route on Form Submit

Hi, I have this action on Home controller
public async Task<IActionResult> Index(EventsFilterOptionsViewModel? filters, bool enableFilter)
public async Task<IActionResult> Index(EventsFilterOptionsViewModel? filters, bool enableFilter)
I want index to accept filter options on filters, but I realized that although that I don't enter any values in input fields, the model filters won't ever be null. So I can't check for filters to be null since it is not even null when the route is /. I was thinking maybe i can add that enableFilter into that action, and then have asp-route-enableFilter on my form,
<form asp-controller="Home" asp-action="Index" method="get" asp-route-enableFilter="true">
<form asp-controller="Home" asp-action="Index" method="get" asp-route-enableFilter="true">
but i also realized even if i submit the form, it does not put enableFilter query string on the route. Is there a way to achieve this? Thanks in advance
13 Replies
Brady Kelly
Brady Kelly2y ago
I don't know much about your problem but normally when I pass a viewmodel back to an action I use POST. Your using GET might just interfere with how the action is invoked with the route parameters
kunio_kun
kunio_kun2y ago
yeah i think it does interfere, but i wish there is a way to just append it somehow For now i used an input type=hidden inside the form, wonder if there's cleaner way for that
when I pass a viewmodel back to an action I use POST
I wanted them to be query strings so in this case filters will persist across refreshes and can be shared over the link, wonder if it's a bad idea..
Brady Kelly
Brady Kelly2y ago
How big is the form? You might be able to serialize it into part of the query string
kunio_kun
kunio_kun2y ago
How big is the form
Is it the model size or the view or..?
Brady Kelly
Brady Kelly2y ago
I mean how many properties/input on the form, so yeah, the model size is probably a good indication
kunio_kun
kunio_kun2y ago
sorry for the late response
using System.ComponentModel.DataAnnotations;
using RemoteMonitor.Enums;

namespace RemoteMonitor.Models;

public class EventsFilterOptionsViewModel
{
public bool EnableFilter { get; set; }
[Display(Name = "Starting Time")] public DateTime? StartDateTime { get; set; }
[Display(Name = "Ending Time")] public DateTime? EndDateTime { get; set; }

[Display(Name = "Infos")]public bool IncludeInfo { get; set; }
[Display(Name = "Warning")] public bool IncludeWarning { get; set; }
[Display(Name = "Error")]public bool IncludeError { get; set; }
[Display(Name = "System")]public bool IncludeSystem { get; set; }
[Display(Name = "Containing Message")]public string? ContainingMessage { get; set; }
[Display(Name = "Order By")] public EventsFilterOrderBy OrderBy { get; set; }

}
using System.ComponentModel.DataAnnotations;
using RemoteMonitor.Enums;

namespace RemoteMonitor.Models;

public class EventsFilterOptionsViewModel
{
public bool EnableFilter { get; set; }
[Display(Name = "Starting Time")] public DateTime? StartDateTime { get; set; }
[Display(Name = "Ending Time")] public DateTime? EndDateTime { get; set; }

[Display(Name = "Infos")]public bool IncludeInfo { get; set; }
[Display(Name = "Warning")] public bool IncludeWarning { get; set; }
[Display(Name = "Error")]public bool IncludeError { get; set; }
[Display(Name = "System")]public bool IncludeSystem { get; set; }
[Display(Name = "Containing Message")]public string? ContainingMessage { get; set; }
[Display(Name = "Order By")] public EventsFilterOrderBy OrderBy { get; set; }

}
maybe it wont be considered big would you mind to point out how this could be done?
Brady Kelly
Brady Kelly2y ago
Hmm, that's a not of non-string properties - I had a few strings in mind. Let me give this some thought Maybe try using the [FromForm] attribute on your enableFilter parameter, i.e.
public async Task<IActionResult> Index(EventsFilterOptionsViewModel? filters, [FromForm] bool enableFilter)
public async Task<IActionResult> Index(EventsFilterOptionsViewModel? filters, [FromForm] bool enableFilter)
kunio_kun
kunio_kun2y ago
I actually wanted to have enableFilter not in the viewmodel. And since it's not in the viewmodel, it's not in the querystring, and not in the form What i tried to achieve was have everything except enableFilter in viewmodel, and then on the submit, i want to append a querystring and have it bound to enableFilter parameter in the controller action
Brady Kelly
Brady Kelly2y ago
It doesn't have to be in the viewmodel for it to be on the form. The whole form data is sent when you submit, then the model binder takes from that what it needs to build the incoming viewmodel. [FromForm] tells the action invoker that the enableFilter property is not a query parameter but a field in the form data If you really want enableFilter to be sent from the browser as part of the query string I thing you're going to have to use JS to catch the form submit event and actually append the enableFilter value to the query yourself
kunio_kun
kunio_kun2y ago
and that would need AJAX or XHR or fetch right?
Brady Kelly
Brady Kelly2y ago
No, you just need JS to manipulate the outgoing POST request, not to make another request
kunio_kun
kunio_kun2y ago
i see. Would you mind to point out what method it is? I couldn't find any reference that does that
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.