MVC 控制器重定向到 Jquery 选项卡

发布于 2024-10-22 03:44:23 字数 1046 浏览 2 评论 0原文

我在 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);
        }

我尝试重定向到的网址是

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

当前代码 ReturnToAction("Index" + "/" + id, Client") 重定向到

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

的不是我想要的。我也尝试过 Redirect(" Client/Index/" + id + "#contacts") 但我得到的 url 是这样的:

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

如您所见,问题是我正在从联系人控制器内部调用客户端控制器生成的页面。Id 工作正常在所有的例子中,

请帮忙!

I have a controller in MVC that after successful create command needs to redirect the view to a contact tab made by jquery. Here is what I have for controller:

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

The url I am trying to redirect to is

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

Current code ReturnToAction("Index" + "/" + id, Client") redirects to

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

which is not what I want. I have also tried Redirect("Client/Index/" + id + "#contacts") but the url I get is something like this:

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

As you can see, the problem is that I am calling a page generated with client controller from inside the contact controller. Id works right in all examples.

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-10-29 03:44:23

我认为你可以做类似的事情。

视图

 $('#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);
 }

I think that you can do something like that.

View

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

Controller

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);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文