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")开发者_StackOverflow社区 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!
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);
}
精彩评论