asp.net-mvc 如何?控制器不返回任何内容/当前视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/579390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:20:59  来源:igfitidea点击:

HOW? Controller return nothing/current view

asp.net-mvc

提问by Dent Argue

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.

LONG:
我有一个局部视图,我在其中创建了一个图像滑块。它包含一个链接,该链接向控制器发送请求以获取下一个图像(使用 ajax)。控制器获取下一张图像,将其存储在 ViewData 中并发送回局部视图(上面的那个)。

现在,我今天要做的是,当控制器到达最后一个图像时,它重新返回完全相同的图像(通过重新获取它),但仍然创建一个新视图,即客户端/浏览器重新解析“相同”数据。
这似乎有些不理想。

我想做的是,当控制器到达最后一个图像时,它应该什么都不做。
如果我返回 null,则视图将更新为空内容。
我希望视图/客户端/浏览器保留它所拥有的一切,而控制器则什么都不做。

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

回答by John Foster

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

如果你不想做任何事情,你可以返回一个 EmptyResult ......

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:-

如果您正在使用 AjaxHelper,您可以通过提供不成功的状态代码(例如 404 或任何最合适的代码)来避免更新,这将阻止它替换您的 div,因为 MicrosoftMvcAjax.js 中的 javascript 在之前明确检查成功响应更新任何元素:-

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

最终避免它的最佳方法是设计局部视图,以便首先避免问题(就像你自己提到的那样)。

回答by Jim Berg

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.

返回状态码 204 基本上对浏览器中显示的页面没有任何影响。当操作完成时,实际结果最终会返回给浏览器。

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.

这个问题很老,但我无法在 StackOverflow 上的任何地方找到答案。我认为它必须是可能的,因为除了保存文件之外,FileResult 也不会真正影响当前页面。

回答by Junior Cortenbach

I would use

我会用

return new HttpStatusCodeResult(204);

this way you would stay on the same page and there is no post back. Here is the defination

这样,您将停留在同一页面上,并且不会回帖。这是定义

The HTTP 204No Content success status response code indicates that the request has succeeded, but that the client doesn't need to go away from its current page

HTTP 204No Content 成功状态响应代码表示请求已成功,但客户端不需要离开其当前页面

回答by tvanfosson

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.

假设您正在使用 MicrosoftMvcAjax,您可以发回一个 JavascriptResult,提醒用户他们已到达滑块的末尾。如果响应是 javascript 而不是内容,MicrosoftMvcAjax 处理程序将执行它而不是替换 DOM 内容。

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

当然,您可能想要更有创意并使用 jQuery 对话框插件或其他东西而不是警报。

回答by Dent Argue

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

好的,我知道了。
不是我的问题的解决方案,但仍然解决了问题。

当视图显示最后一个图像时,我不会显示“下一步”链接。

不敢相信我之前没有想到……

谢谢你们的努力