简体   繁体   English

如何从控制器重定向到另一个视图

[英]How to redirect to another view from controller

I want to redirect to a view after I post a function from controller. 从控制器发布函数后,我想重定向到视图。 Here is my codes: 这是我的代码:

 function CreateCollectivePdf() {
        var objectList = $(".hdnProductId");    
        var selectedProductIdList = '';    
        $(".hdnProductId").each(function (index) {
            var singleElement = objectList[index];
            selectedProductIdList += singleElement.value + ',';
        });

        $.ajax({
            type: "POST",
            url: '@Url.Action("CreateEBulletinPdf","EBulletin")',
            contentType: "application/json",
            dataType: "JSON",
            data: JSON.stringify({ "productIdList": selectedProductIdList }),
            success: function (result) {

            }
        });

    }

Controller : 控制器:

  public RedirectToRouteResult CreateEBulletinPdf(string productIdList)
        {
            try
            {
                productIdList = productIdList.Substring(0, productIdList.Length - 1);

                var productIdStrings = productIdList.Split(',');


                detailViewModels = productIdStrings.Select(productIdString => PdfProduct(Convert.ToInt32(productIdString))).ToList();


                return RedirectToAction("ProductsEBulletin");
            }
            catch (Exception ex)
            {

                throw;
            }
        }
public ActionResult ProductsEBulletin(List<ProductDetailViewModel> _detailViewModels)
    {
        try
        {
            return View(_detailViewModels);
        }
        catch (Exception)
        {

            throw;
        }
    }

There is no exception but my ProductsEBulletin view wont be shown. 也不例外,但不会显示我的ProductsEBulletin视图。 Do you have any idea where is my mistake? 你知道我的错误在哪里吗?

Well, you've gotta do something in this success AJAX function. 好吧,您必须在success AJAX功能中做些事情。 Right now you've left it blank, so you cannot possibly expect something to happen: 现在,您将其留空,因此您不可能期望会发生任何事情:

success: function (result) {

}

You probably want to show this resulting html somewhere in your DOM in order to see the results popping up somewhere. 您可能希望在DOM中的某个位置显示此结果html,以查看结果在某个位置弹出。 For example inject it in some container: 例如,将其注入某个容器中:

success: function (result) {
    $('#someDivId').html(result);
}

Now of course showing entire HTML with header inside a div element will break your DOM. 当然,现在显示整个HTML及其div元素内的标头将破坏您的DOM。 You probably want to return only a partial view from your server in this case without including the entire Layout: 在这种情况下,您可能只想从服务器返回部分视图,而不包括整个Layout:

public ActionResult ProductsEBulletin(List<ProductDetailViewModel> _detailViewModels)
{
    return PartialView(_detailViewModels);
}

You can also notice that I have removed the useless try/catch statement from your ProductsEBulletin action because all it was doing at the moment was propagating the exception which is not very constructive. 您还可以注意到,我已经从您的ProductsEBulletin动作中删除了无用的try / catch语句,因为此刻它所做的只是传播异常的语句,该异常不是非常有建设性的。

OK, now only this partial will be injected into some container that you need to place somewhere in your DOM: 好的,现在只将这个部分注入到您需要放置在DOM中的某个容器中:

<div id="someDivId"></div>

This being said, if you need to redirect to an entirely new location, then probably AJAX is not something that you should have used in the first place but rather a standard HTML form postback. 这就是说,如果您需要重定向到一个全新的位置,那么AJAX可能不是您应该首先使用的东西,而是标准的HTML表单回发。

Your view won't be shown because the ajax function is not capable of getting the view response and then displaying it to the browser. 您的视图将不会显示,因为ajax函数无法获取视图响应,然后将其显示给浏览器。 I usually solve this by creating a form that posts to your controller's url. 我通常通过创建一个发布到控制器的URL的表单来解决此问题。 Before sending, you can programatically add an input to a form in your CreateCollectivePdf() function and then submit it so you can process your comma separated values in your function and then render your view. 在发送之前,您可以以编程方式在CreateCollectivePdf()函数中向表单添加输入,然后提交它,以便可以在函数中处理逗号分隔的值,然后呈现视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM