HOW? Controller return nothing/current view

return empty actionresult
mvc return empty file result
return view in mvc
mvc actionresult stay on current page
actionresult without return
return string from controller to view
redirect to another view in mvc
actionresult c#

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");
    }

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).

Return no ActionResult, Or in other words, I would like to stay on the current View without tried this but the Action is returning the original View as an empty page. mvc. return View () is basically a function inside the Controller class which returns an instance of ViewResult. ViewResult view=new ViewResult(); return view; More about ViewBag. How ViewBag in ASP.NET MVC works. improve this answer. edited Dec 13 '17 at 21:30. 2 silver badges. 8 bronze badges. answered Mar 3 '14 at 8:57. 44 silver badges.

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.

EmptyResult Return Type In ASP.NET MVC 5, MVC controller returns many types of output to the view according to in MVC which does not return anything at client site, its just like Void method . ToString: This method is used to convert the current result to the string . Yes. By default, ASP.NET MVC checks first in \Views\ [Controller_Dir]\, but after that, if it doesn't find the view, it checks in \Views\Shared. The shared directory is there specifically to share Views across multiple controllers. Just add your View to the Shared subdirectory and you're good to go. If you do return View ("~/Views/Wherever

Assuming that you are using MicrosoftMvcAjax, you could send back a JavascriptResult that alerts the user that they have reached the end of the slider. If the response is javascript rather than content, the MicrosoftMvcAjax handler executes it instead of replacing the DOM contents.

[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
        {
            return JavaScript( "alert('No more images');" );
        }
    }

    return RedirectToAction("Error", "Home");
}

Of course, you'd probably want to be more creative and use the jQuery dialog plugin or something rather than an alert.

Return View With ViewName in Controller Sample in MVC, This article describes returning a viewname from a controller. Step 1. Create a MVC project from the "Empty" template. Right-click on "Controllers"  The partial view is nothing but pagelet, that we can return from the controller and that merges with the main view and generates one concrete HTML page. It may take 4 parameters to render in the partial view. Redirect This is equivalent to Response.redirect () or Server.Transfer () functions.

Ok, I've got it. Not a solution to my question but still solves the problem. I'll simply not show the "Next" link when the view shows the last image. Can't believe I didn't think of it earlier... Thanks for your efforts guys

Working with ActionResult in ASP.NET MVC, We can also specify an action to return a partial view instead of a regular view: Unless you return null, in which case MVC returns an EmptyResult. in the current controller if you don't specify which controller to look in. Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button. Step 3 Name the controller as in the following: Step 4 Now we need to create a view. Right-click on "Index" and select "Add View". Step 5 Change the view name from "Index" to your desired name. By default it considers an action method name as the view name.

Views in ASP.NET Core MVC, NET Core MVC, views are .cshtml files that use the C# programming language in The default behavior of the View method ( return View(); ) is to return a view Similarly, you can indicate the current controller-specific directory with the ". Nothing prevents you from using the same classes for both your  public ActionResult Login() { return View(); } Important Note. The View() method doesn't make new requests, it just renders the view without changing URLs in the browser's address bar. The RedirectToAction() method makes new requests and URL in the browser's address bar is updated with the generated URL by MVC.

Async Return Types (C#), Task<TResult> Return Type; Task Return Type; Void return type For more information about async methods, see Asynchronous Programming with async and await (C#). WriteLine("The current temperature is 76 degrees. A look at the Sitecore MVC execution lifecycle. Although there is nothing special about the controller you create to use with a controller rendering, the point at which that code is executed differs from the norm. Let’s say you have a page with 1 view rendering and 1 controller rendering.

Controller (Symfony Docs), line 16: The controller creates and returns a Response object. Mapping a URL to a Controller¶. In order to view the result of this controller, you need to map a URL​  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 . Key points. It does not return any output to the browser. It shows the empty result set to the browser without adding the view. Does not require to add the view.

Comments
  • Just like what I am looking for, but no answer yet. The below one is refreshing page, that I even dont like. as my javascript/jquery updated the user interface and there is nothing from server side to update on UI that just expect a flag to see if my controller is successful in executing some task or not. if yes, i update the UI with javascript/jquery, if not then again update certain thing (like a check mark) with it only. All other page content I like to remain as it was without refresh effect that kill javascript changes for user actions. The below ans. not working as it return blank view.
  • Well actully it does something, it returns an empty result. Same as returning null. This simply shows empty content. That is not what I want it to do. I want it to.. hmm.. do nothing, bail out, leave current view as is. No update. :)
  • Well you're calling it via AJAX so it has to receive a response from the XMLHttpRequest? Just check the length of the result in the success callback and if it's greater than 0 replace the contents with your new HTML fragment otherwise leave it as is.
  • I'm using the "built-in" ajax: <%= Ajax.ActionLink("Next", "GetNextImage", "Image", new { ... }, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "ImageDiv" })%> Was hoping for a simple solution with no need for gritty javascripting etc.
  • return new HttpStatusCodeResult(204) was the answer I was looking for. return new EmptyResult() generated a blank page.
  • return JavaScript( "alert('No more images');" ); results in my view changing to plaintext saying alert('No more Images');