I have a ASP.NET Core 2.0 web application with a mix of Razor Pages and MVC. When I do a POST to the page, I often want to redirect the user back to the same page, but with a GET request.
In ASP.NET Core 2.0 Razor Pages, I typically do it like this:
public async Task<IActionResult> OnPostView(...)
{
// Update based on POST data
...
return RedirectToPage(); // Redirect back to GET
}
What is the equivalent to this in a MVC controller?
The equivalent method is:
return RedirectToAction();
See the source for more information.
Related
To give a more appish experience to my asp.net mvc application, I would like to make the links to other pages (actions in mvc) be loaded using ajax (displaying a loading sign while the request is being processed). I have been quite successful doing this with jQuery Mobile but for the project that I am doing now I prefer not to use it. In part this is because as soon as loading jQuery Mobile css the previous look of the site is disrupted.
Is there a similar page mechanism in asp.net? It would be even better if features such as transitions and pre-rendering of pages would be available too, as jQuery Mobile does.
An example of what I try to achieve
I have a controller like this:
public ActionResult List()
{
return View();
}
public ActionResult Dashboard()
{
return View();
}
Then on the dashboard view a link:
LIST
Clicking the link would request the list page from the server and start a slide effect to transition from one page to the other.
Thanks for your ideas and knowledge on that!
I'm working on a project converting older .aspx pages to newer MVC pages. I'm currently using the routing function to map the existing legacy pages to use the newer MVC pages behind the scenes. This is working great because it is preserving the existing URL, however, whenever the controller needs to redirect the user OR an MVC Url.Action link is used, the URL it sends the user to is the MVC url and not the routed legacy page url. I realize that this is how its suppose to be functioning out of the box but I was wondering if MVC has "reverse routing" functionality of some sort. By reverse routing I am looking for something that allows the Url.Action("action") to return the routed .aspx associated url with the specified action instead of the default /controller/action link.
So for instance,
here is a sample route
context.MapRoute("AboutUs", "pages/aboutus.aspx", new { controller = "default", action = "aboutus" });
This enables domain.com/pages/aboutus.aspx to run the new MVC action "aboutus" from the DefaultController behind the scenes perfectly.
However, if on another MVC page I have a link created by
Url.Action("aboutus")
it points them to domain.com/mvc/aboutus. I would like it to point them to domain.com/pages/aboutus.aspx to keep uniformity across the site.
Thanks for any insights that you can provide. I might end up having to override the Url.Action method to look up the route and return the mapped url but I just wanted to check for existing functionality so I don't reinvent the wheel.
Try using the RouteUrl extension method, which matches the route by route name rather than route parameters, like so:
Url.RouteUrl("aboutus") // route name
MSDN: http://msdn.microsoft.com/en-us/library/dd460347
I m beginner asp.net mvc developer.And I don't understand,
Why has used both Json and ActionResult in Account controller(for register, login and etc.) in default asp.net MVC 4 project?
Thanks.
It's because in the ASP.NET MVC 4 the Login and Register forms are shown in a jQuery dialog and both forms POST to the Account controller using AJAX. The other actions that return views are similar to what existed in previous versions. For example if the user has javascript disabled he won't be able to use jQuery dialog and AJAX and he will fallback to standard HTML.
Do you means having both JsonResult and ActionResult for (register and login view) in default MVC 4 template?
It means two views for both Register & Login form. One is using with ajax and the other is as normal view.
eg: Click on "Register" link on top, it will popup ajax "Register" view.
Click on "Login" link on top -> click "Register", it will show as normal view.
Alariza,
As Darin pointed out, there are two action methods for each form (Register & Login). One action method is used for the popup version of the view and the other is for the normal view.
Don't get discouraged by the negative comments.
I have MVC3 app with form for edit data. This for include only dropdownlist.
It is any posibility to execute Controller method (HttpPost) without reload?
If not - how Can I return current site (because I have the same form in different sites).
Yes, try XmlHttpRequest better known as AJAX to execute controllers. It's like sending a request from the browser to the server on a background thread which won't cause a page reload.
have a read of these blogs:
http://www.nikhilk.net/Ajax-MVC.aspx
http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-2-0-and-AJAX-Part-1.aspx
If you can't use ajax, to get back to the same page you can redirect the user back to the referrer url:
public ActionResult Submit()
{
// do something
return Redirect(Request.UrlReferrer.ToString());
}
In ASP.NET webforms I used to write AJAX apps where the client would communicate to the server using web services or page methods and JSON.
I'm new to ASP.NET MVC and have found that one could use web services or controller actions that use JSON. Are there other options? Should I use web services or controller actions and why?
Most MVC apps use controller actions for Ajax actions.
I think the "why" is that you're leveraging your skillset and the MVC infrastructure. Everything you're doing in the app is following the MVC model, why not drop Ajax into it as well? It's just a very natural pattern.
On the server, I often find myself forgetting if an action is Ajax invoked or not. I think this is a very good thing. The method of invoking the action is a separate concern from what the action does. This is particularly true, when you're using progressive enhancement (Ajax for Javascript enabled, http post for non-js). Then the pattern looks something like below. I really like that I can forget about the communication mechanism and focus on what my app is supposed to do.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyProgressiveEnhancedAction(FormCollection form)
{
// Handle Html Post
if (!Request.IsAjaxRequest())
return ActionViaHtmlPost();
// Ajax Invocation
...
When I'm using this pattern I often find that I have a little bit of glue to abstract Ajax vs. Post and the rest (model, domain interactions, etc.) are common.