0

我在MVC中有一个控制器,在成功创建命令后需要将视图重定向到由jquery制作的联系人选项卡。以下是我对控制器:MVC控制器重定向到jquery选项卡

[HttpPost] 
    public ActionResult Create(Contact newContact, int id) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       gogoDB.AddToContacts(newContact); 
       gogoDB.SaveChanges(); 

       return RedirectToAction("Index" + "/" + id, "Client"); 
      } 
      else 
      { 
       return View(newContact); 
      } 
     } 
     catch 
     { 
      return View(newContact); 
     } 

我想重定向的URL是

http://localhost:xxxxx/Client/Index/2#contacts 

当前代码ReturnToAction( “指数” + “/” + ID,客户“)重定向到。

http://localhost:xxxxx/Client/Index/2 

这不是我想要什么,我自己也尝试重定向(“客户端/索引/” + ID +“#contacts”),但我得到的网址是这样的:

http://localhost:xxxxx/Contact/Create/Client/Index/2#contacts 

正如您所看到的,问题在于我正在使用客户端控制器从联系人控制器内部调用生成的页面。 Id适用于所有示例。

请帮忙!

+0

您是否尝试过使用返回永久重定向()和硬编码的标签的网址是什么? – James 2011-03-16 07:05:34

+0

硬编码将无法正常工作,因为返回网址中有一个动态生成的ID。 – user527662 2011-03-16 07:56:11

回答

0

我认为你可以做那样的事情。

查看

$('#SpecificRedirectTest').click(function() { 
window.location="/Controller/Index?Id="+ $('myId').val() +"&redirect2Tab=true"; 
    }); 

控制器

public ActionResult Index (int Id , bool? redirect2Tab) 
{ 
    var client = this._myService.GetById (Id); 
    var fvm = this.EditClientFormViewModel (client, MyTabs.EditTab); 

if (redirect2Tab.HasValue) { 
    if (redirect2Tab.Value) { 
    fvm = this.EditClientFormViewModel (client, MyTabs.SalesHistoryTab); 
    } 
} 
return View("ViewEdit", fvm); 
} 
+0

感谢您的回应Koi,但我们离开了部分视图,因此我无法测试解决方案,因为我们不再使用它们。 – user527662 2011-04-04 04:36:10

相关问题