Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOW? Controller return nothing/current view

Tags:

asp.net-mvc

SHORT:
How do I make a controller return the current view or just simply do nothing?

LONG:
I have a partial view where i've created an imageslider. It contains a link which sends a request to a controller to get the next image (using ajax). The controller fetches the next image, stores it in ViewData and sends back a partial view (the one above).

Now, what I do today is that when the controller reaches the last image it re-return the very same image (by refetching it), but still creates a new view, that is, the client/browser re-parses the "same" data.
This seems somewhat non-optimal.

What I'd like to do is that when controller reaches the last image it should simply do nothing.
If I return null then the view is updated with empty contents.
I want the view/client/browser to retain whatever it has and the controller to simply do nothing.

    [AcceptVerbs(HttpVerbs.Post)]     public ActionResult GetNextImage(...)     {         if(Request.IsAjaxRequest())         {             if(CURRENT_IMAGE != LAST_IMAGE)             {                 Image image = GetNextImage(...);                 var partialViewResult = new PartialViewResult();                 partialViewResult.ViewName = "ImageSlide";                 partialViewResult.ViewData.Model = image;                 return partialViewResult;             }             else             {                 // DO NOTHING, HOW?             }         }          return RedirectToAction("Error", "Home");     } 
like image 621
Dent Argue Avatar asked Feb 23 '09 21:02

Dent Argue


People also ask

How do I return an empty result?

EmptyResult is used when you want to execute logic return inside the controller action method but does not want any result back to the view then EmptyResult return type is very important . It does not return any output to the browser. It shows the empty result set to the browser without adding the view.

Can we return blank view in MVC?

@RobinMaben: No, null would not return an object from the method.

How do I return nothing in ActionResult?

There are two ways to return NULL from an ActionResult (Action Method): 1. Using EmptyResult class. In order to learn more about EmptyResult class, please refer my article ASP.Net MVC EmptyResult Example: Return NULL (Nothing) from Controller to View.

How do I return a view from a controller?

How to return a view from controller action method? To return a view from the controller action method, we can use View() method by passing respective parameters. return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required.

How to access selected objects of a view from a controller?

A less common task is accessing focused and selected objects of a View from a Controller. In this instance, you should use the View.CurrentObject and View.SelectedObjects properties of the View object specified by the ViewController.View property.

How to return null (Nothing) from actionresult (action method) in MVC Razor?

Simply return NULL value. In such case, ASP.Net automatically returns EmptyResult class object internally. In this article I will explain with an example, how to return NULL (Nothing) from ActionResult (Action Method) in ASP.Net MVC Razor. 1. Using EmptyResult class.

What is the current object of the active view?

The CurrentObject property specifies the current object of the active View. If an active View is a List View, this property specifies the focused object. If the View is a Detail View, the property specifies the object displayed by it.

How to make an action return a void?

no, you can not make an action return void. an action must be of type ActionResult. if you change the type to void, it can not be called from the browser. instead of a view, you can return content, or json. see docs. an achor, replaces the current page with replacement html. if you don't want to do this, you must use ajax. see jquery docs.


2 Answers

You can return an EmptyResult if you want it to do nothing...

return new EmptyResult(); 

If you're using the AjaxHelper you can avoid the update by supplying an unsuccessful status code (e.g. 404 or whatever is most appropriate), that'll stop it replacing your div as the javascript in MicrosoftMvcAjax.js explicitly checks for a successful response before updating any elements:-

this.ControllerContext.HttpContext.Response.StatusCode = 404; return new EmptyResult(); 

Ultimately the best way to avoid it is to design the partial view so it avoids the problem in the first place (like you mention yourself).

like image 103
John Foster Avatar answered Sep 30 '22 20:09

John Foster


I ran into this problem today. I wanted to find a solution for how to deal with double-clicks on the client side trying to reenter the controller action on the server side while it was still processing. If a user entered that action, I wanted it to just ignore the request and do nothing on the browser side.

Solution looks like this:

public async Task<ActionResult> MyAction() {     if(!CanEnterAction(nameof(MyAction))) return new HttpStatusCodeResult(204);     try     {          // Do long running stuff          return ValidActionResult();     }     finally     {         ExitedAction(nameof(MyAction));     } } 

Returning a status code of 204 basically does nothing to the page displayed in the browser. The actual result eventually makes it back to the browser when the action is complete.

This question is old, but I wasn't able to find an answer anywhere on StackOverflow. I figured it had to be possible since a FileResult doesn't really affect the current page, either, other than saving a file.

like image 20
Jim Berg Avatar answered Sep 30 '22 20:09

Jim Berg