[英]Redirection to a another page asp.net mvc
I am facing a problem, that occurs when I want to redirect the user after the create page, to another page that is not the index page. 我遇到一个问题,当我想在创建页面后将用户重定向到不是索引页面的另一个页面时,会发生此问题。
I have tried to create another view in the Client folder and then replace "index" by "success" the name of the new page for example. 我尝试在Client文件夹中创建另一个视图,然后将“ index”替换为“成功”新页面的名称。
[HttpPost]
public ActionResult Create(Client client)
{
if (ModelState.IsValid)
{
db.Clients.Add(client);
db.SaveChanges();
return RedirectToAction("Index"); // Here is the problem
}
}
I also try Redirect("~/Client/Success")
but it doesn't work also. 我也尝试了Redirect("~/Client/Success")
但是它也不起作用。
Thank you for your help! 谢谢您的帮助!
You need to create not only view named "Success" (actually that isn't required at all), but an action named "Success" in your controller: 您不仅需要创建名为“ Success”的视图(实际上根本不需要),还需要在控制器中创建一个名为“ Success”的操作 :
[HttpPost]
public ActionResult Create(Client client)
{
if (ModelState.IsValid)
{
db.Clients.Add(client);
db.SaveChanges();
return RedirectToAction("Success");
}
return View(); //Looks like you've missed this line because it shouldn't have compiled if result isn't returned in all code branches.
}
public ActionResult Success(Client client)
{
//...
return View();//By default it will use name of the Action ("Success") as view name. You can specify different View if you need though.
}
But I wouldn't say it's a good idea to use redirect just to show the success result . 但是我不会说仅使用重定向来显示成功结果是一个好主意 。 You better create the Success view in the Client folder as you've done already (assuming your controller is named "ClientController") and then return View result instead of Redirect: 您最好像已经完成的那样在Client文件夹中创建Success视图(假设您的控制器名为“ ClientController”),然后返回View结果而不是Redirect:
[HttpPost]
public ActionResult Create(Client client)
{
if (ModelState.IsValid)
{
db.Clients.Add(client);
db.SaveChanges();
return View("Success");
}
return View(); //Looks like you've missed this line because it shouldn't have compiled if result isn't returned in all code branches.
}
This will work : 这将起作用:
return RedirectToAction("Success","Client");
where Success
is your action name
and Client
is your controller name
其中Success
是您的action name
, Client
是您的controller name
if you want to redirect to Success action in same controller then : 如果要重定向到同一控制器中的“成功”操作,则:
return RedirectToAction("Success");
Your action should look like this in ClientController : 您的操作在ClientController中应如下所示:
public ActionResult Success()
{
return View();
}
You are using : 您正在使用 :
return RedirectToAction("Index");
this will redirect you to Index
actionresult
in same controller
. 这会将您重定向到同一controller
Index
actionresult
。
Take a look at the overloads for that method . 看一下该方法的重载 。 You're not redirecting to a URL , you're redirecting to an action . 您没有重定向到URL ,而是重定向到操作 。 So this will redirect to the Index
action on the current controller: 因此,这将重定向到当前控制器上的Index
操作:
return RedirectToAction("Index");
Or this will redirect to the Success
action on the Client
controller: 或这将重定向到Client
控制器上的Success
操作:
return RedirectToAction("Success", "Client");
You can try a Ajax resquest like that : 您可以尝试这样的Ajax请求:
$('#myButton').click(function () {
var url = '/MyControllerName/MyPage';
var $Param = $('#SomeParam').val();
$.ajax({
url: url,
type: 'POST',
cache: false,
data: {
Param: $Param
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.