❔ ASP.NET Core MVC Put Value to Route on Form Submit
Hi,
I have this action on
Home
controller
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,
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 advance13 Replies
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
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 POSTI 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..
How big is the form? You might be able to serialize it into part of the query string
How big is the formIs it the model size or the view or..?
I mean how many properties/input on the form, so yeah, the model size is probably a good indication
sorry for the late response
maybe it wont be considered big
would you mind to point out how this could be done?
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.
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 actionIt 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 yourselfand that would need AJAX or XHR or fetch right?
No, you just need JS to manipulate the outgoing POST request, not to make another request
i see. Would you mind to point out what method it is? I couldn't find any reference that does that
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.