Call an Action from an MVC View - asp.net-mvc-3

I'm trying to figure out how to call an action from an MVC view, not call a view URL.
I have an Action Method for signing off in the Account Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SignOff()
{
authenticationService.SignOut();
return View("SignOff");
}
And I'd like to call it from a cshtml page, like this:
<li>#Html.Action("SignOff","Account")</li>
The list is part of a drop down menu.
ActionLink doesn't work because I don't have an Account/SignOff page. The above code returns the following: "Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'." ActionLink just tells me the resource doesn't exist.
Is there anyway to do this? I just want users to be able to select a "Logout" item from a menu and then have the application call the SignOff method. Do I need to use Ajax? Javascript? I'm still experimenting. Thank yoU!
EDIT: Ok, it was the http.post that was keeping me from calling this directly. I got rid of the acceptverbs attribute and now I can call the method directly.

You can just redirect to the Login page or to any other public page (home page).
return RedirectToAction("Login");

Related

Play Framework 2.x : Ajax request to controller without page refresh

I have a controller LessonController with an action save(). My route for this action is POST /save LessonController.save(name: String). The code is very simple for this action.
public static Result save(String name){
Lesson lesson = new Lesson();
lesson.setTitle(name);
lesson.save();
return ok(lesson.getLessonId().toString());
}
Here is my ajax call from the view
myJsRoutes.controllers.LessonController.save(name).ajax({
success : function(id){
alert('Success');
}
});
This code does create a lesson but it refreshes the page and does not execute the success block from ajax request. I guess it is because I am returning a Result type back. I tried changing the return type to String but it gave me a compilation error.
So the question is, How can I call an action in a controller with ajax without the page refresh behaviour?
If you get a page refresh from that then it is for some other reason. Like for example triggering the ajax call with a submit button and not consuming the button event so that it leads to a form submit POST or GET
Please check you AJAX call event, may but you are triggering the AJAX call using submit button or may be you are submitting any form in the web page with the same event.

Controller redirect back to a POST form

I have a table showing a list of names, with an "edit" button and hidden id value at the side.
Clicking the "edit" button will post the hidden id as a form value and display the edit page so the user can change details of that person. Pretty standard.
When editing the details and submitting I'm using a validator. If the validation fails it needs to go back to the edit page and display the errors. Problem is that the edit page required an Id value via POST method, but the redirect only seems to utilise the GET method which leads to a "Controller Method Not Found" error as there is no Get route set up.
Does anyone know how I can redirect back to the page via POST not GET. Currently my code looks like:
public function postEditsave(){
...
if ($validator->fails())
{
return Redirect::to('admin/baserate/edit')
->withErrors($validator)
->withInput();
}else{
...
thanks
You can use Redirect::back()->withInput();
You may wish to redirect the user to their previous location, for example, after a form submission. You can do so by using the back method
See: http://laravel.com/docs/5.0/responses
You don't need to use POST to go to the edit page. You can use GET and a parameter to the route, check this out: http://laravel.com/docs/routing#route-parameters
You'd have a GET route to show the edit page, and a POST route to handle the request when the user submits the form.
It'll look like this (notice the parameters):
public function getEdit($id)
{
return View::make(....);
}
public function postEdit($id)
{
...
return Redirect::back()->withErrors($validator)->withInput();
}
if "redirect with POST" is exist, then I don't know it. I recomend you to just use flash data
Redirect::to('user/login')->with('id', 'something');
You can use Redirect::to("dashboard/user/$id")->withErrors($validator)->withInput();. You should use double quote to pass parameter if there is any errors with validation.

Why is PartialView not called when being called from another ActionResult method?

Im using MVC4.
I have a [HttpPost] controller method that returns an ActionResult.
In this function, when I catch an exception, my plan is to call a [ChildActionOnly] ActionResult method which returns PartialView("ExceptionMessage", exceptionObject).
ExceptionMessage.cshtml basically renders a markup with the full Exception object.
Now for some reason, when this PartialView is invoked, the breakpoint within the .cshtml is not being hit.
Why does this not work?
It was a user error on my part.
The view is called when the controller method returns, and thus gets a chance to render the cshtml.

ASP.Net MVC 3: Multiple views/urls to one controller action

I'm trying to do the following with ASP.Net MVC 3:
I have a lot of "flat pages", which are basically html documents with no dot.net code attached.
I want to be able to request these pages through routed URLs, but I do not want to manually add each url to the routes.
So my question is: Is it possible to define a default route, which uses the same controller / action, but returns a view based on the URL requested ?
e.g. /home/about and /profile would use the views /home/about.cshtml and /profile.cshtml
but both would use the same controller and action, which pretty much just goes:
return View();
The reason: I'm doing all the pages of the site, which require dot.net code. However another person is doing all the "flat pages" (informative pages, etc.).
I want him to be able to add new pages, by just adding a cshtml file (like he would with webforms creating aspx files, with no code-behind)
This is necessary because I'd otherwise have to edit global.asax each and everytime he adds a page, which is quite often.
If this is not possible, I'll have to stick with webforms, which I really don't want to :-(
You can make an action that takes as a parameter the name of the View; Something like this:
public ActionResult StaticPage(string viewName)
{
return View(viewName);
}
Then define a route so the viewName isn't a parameter but instead is part of the URL:
"/Static/{viewName}"

How to redirect users based on what page they came from?

I have a page (Controller Action that renders a View) that a user could navigate to from 3 different pages. Basically, a user gets there, does a few selections and clicks on Save button. At this point, I need to redirect the user to where he came from.
I'm wondering, what's the best practice to do that?
I know, for example, I could look in Request, figure out where he came from, then redirect back to there... But that doesn't look like the ideal approach to me.
I'm wondering, what's the best practice to do that?
Pass a returnUrl parameter when invoking this action. Store it in a hidden field if necessary. Make sure that the controller action that performs the validation and needs to redirect gets this parameter as action argument somehow and when the time comes return Redirect(returnUrl);
(Posted on behalf of the question author).
This is what I ended up doing.
Controller:
public ActionResult Index()
{
ViewBag.Referrer = Request.UrlReferrer.LocalPath;
//.....
return View();
}
View (Razor syntax):
Back

Resources