I have Ajax.ActionLink that POSTS to a method on a controller and passes an Id.
It definitely posts as I have decorated the method with: [HttpPost].
The url is displaying the Id value.
Is this correct, as I would have thought POSTing would hide the Id from the url.
I'm wondering if this is default functionality of MVC3 or whether I need to change my routevalues?
Thanks
Decorating the controller method is not enough, and doesn't look to be the problem in this case.
If the id is showing as part of the URL, the request is likely a GET rather than a POST.
You can specify the request type in the Ajax.ActionLink. Make sure you indicate that it should be a POST.
Do this using AJAX Options.
Related
I am trying to figure out how I can pass model data from a controller to a razor view in a non-mvc Razor Pages web application.
We have a system in process where we currently generate a model, and send it off to an external service which will bind a razor template with the model and send an email. We need to migrate away from the external service and switch over to our existing mail service. I already have the model ready to post to an endpoint, and I have the code that will send an email with the html body. All the examples I have seen for Razor Pages either generate the model in the cshtml.cs file. I can create a web app that has a controller with an endpoint where I can post the json model (which I deserialize to a custom class), and I have razor pages that can bind to my custom class. I just can't figure out how I can go from the controller that receives the request, to passing that data to the Razor page to render the view.
Yeah, after hours of searching I posted this and was presented with a "Does this answer your question" suggestion before posting. I was "No, that is not it." After posting and re-reading, yeah, that was it. :(
Answer here:
How pass objects from one page to another on ASP.Net Core with razor pages?
Let me first be clear about what I'm not asking. I can see that the native ASP.NET MVC Ajax.BeginForm does not handle posting files from within an AJAX form post. There are answers I've seen that seem to establish this fact, and to offer an assortment of workarounds via jQuery plugins, etc:
Ajax.BeginForm in MVC to upload files
File upload with ajax in asp.net mvc
I'm not asking whether it can't be done, and I'm not asking for a workaround. I'm hoping someone can explain why it can't be done.
How is the AJAX form post being handled in MVC such that file inputs do not post? I guess I had always assumed that it was being handled at a base level just like a full post, but with some javascript trimmings that prevented a full page reload. Apparently that's not the case. Is the form submitted via javascript something like jQuery's $('#form').submit()? What's happening under the covers?
So I'm able to successfully integrate AJAX requests with Spring MVC. However, I have a problem- if I click the "submit" button of my form, my #Controller class detects the url and returns a ModelAndView. However, what I want is that there be an AJAX check first, and if the form submission is not successful (e.g., blank fields), return an AJAX response. Otherwise, proceed as per normal and display a ModelAndView. However, I have no clue how to integrate both at the same time.
Any ideas or tutorials are appreciated.
Thanks!
You have several choices:
submit the form to a specific, different URL, when using AJAX
add a specific parameter to the request when posting using AJAX, and use this specific parameter to check if the request is n AJAX request
test if the X-Requested-With request header is present and contains XMLHttpRequest
I would go the PJAX route or what's also known as HiJax.
Basically you return a subset of the page if it's an AJAX request using headers. Most people than just use conditions in their view/template to decide to include the full or chrome-less HTML.
I'm using the standard forms authentication registration that comes with MVC 3.
I want to add a few checkboxes to that form that indicates what kind of user the registrator will be. Depending on the checkbox choice the user will be redirected to another get method where additional information can be added.
The problem is that I don't want these methods to be accessible in any other way (like typing the url for example). Only in this specific case these these methods can be accessed.
My first idea was to create a session in the post method of the first registration form and check in the get method if the session exists. But this seems like a less elegant way.
Does anyone has a safer solution for this problem?
You should be able to use the [ChildActionOnly] attribute: found here on MSDN.
Scott Guthrie blogged about ASP.NET MVC 2: Model Validation little over a year ago and in his post, the controllers were decorated with calls to ModelState.IsValid-method. Since then we've had the ASP.NET MVC 3 which included quite big changes to the validation.
But has the requirement to call the ModelState.IsValid still stayed the same? Are all the DataAnnotation attributes useless if the site visitor has disabled the JavaScript and the site developer has forget to check the value of ModelState.IsValid?
If yes, is there a way around this? Is it for example possible to register a global filter which always remembers to check for the model's validity event if the coder doesn't?
The client side validation features would be turned off. This is why you must never rely only on client side validation. It would not affect the model binder which uses the annotations on the server. Here is the relevant text from that blog post...
Because the action method accepts a
“Person” object as a parameter,
ASP.NET MVC will create a Person
object and automatically map the
incoming form input values to it. As
part of this process, it will also
check to see whether the
DataAnnotation validation attributes
for the Person object are valid. If
everything is valid, then the
ModelState.IsValid check within our
code will return true – in which case
we will (eventually) save the Person
to a database and then redirect back
to the home-page.