【问题标题】:Redirecting to action in ASP.NET MVC, method wont return the view重定向到 ASP.NET MVC 中的操作,方法不会返回视图
【发布时间】:2011-11-01 08:26:09
【问题描述】:

我正在使用 jquery post 方法来执行服务器端操作

方法是:

 function redirectToDraft() {

    updateInboxGridAndCloseApproveDialog();
    $.post('/Personnel/AgendaApproveDocument');
  }

它将发布到这个动作,然后在动作中我需要重定向到另一个控制器上的另一个动作,如下所示:

 public ActionResult AgendaApproveDocument(int? id){

     return RedirectToAction("RelatedDocumentDraft",  new RouteValueDictionary(
                new
                {
                    controller = "RegisterLetter",
                    action = "RelatedDocumentDraft",
                    title = relatedDoc.Name,
                    factory = relatedDoc.DocumentFactory,
                    activityId = action.WorkItem.Id
                }));

}

它工作并重定向到方法:

 public ActionResult RelatedDocumentDraft(int?activityId, string factory, string title)
    {
    return View("~/Views/RegisterLetter/NewDraft.aspx");
    }

我的问题是它没有返回到视图“NewDraft”

有什么问题吗?

【问题讨论】:

  • 您是否尝试过调试以查看您的视图是否真的可以在该位置找到?
  • 那么它被重定向到哪个页面了?或者你得到什么回应?

标签: c# jquery asp.net-mvc


【解决方案1】:

在您的 AJAX 调用中,您尚未订阅成功回调,因此不会发生任何事情。如果您想处理 AJAX 调用的结果,您应该这样做:

$.post('/Personnel/AgendaApproveDocument', function(result) {
    // the result variable here will contain the markup of the view    
    // so you could for example replace some portion of the DOM with it
    // (assuming of course it is a partial view):

    $('#foo').html(result);
});

【讨论】:

    【解决方案2】:

    我认为您正在寻找RedirectToAction 方法。检查来自 John Sheehan 的 this answer

    return RedirectToAction("Index", model);
    

    关于MSDN的更多信息。

    【讨论】:

    • 如果您完整阅读我的帖子,您会发现我的问题是我重定向到的方法不会返回视图
    【解决方案3】:

    您正在对此方法进行 AJAX 发布 - 除非您告诉它,否则它不会对响应做任何事情。为什么是 AJAX,一个简单的表单提交会更好吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多