Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重定向到另一页asp.net mvc_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 重定向到另一页asp.net mvc

C# 重定向到另一页asp.net mvc,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我面临一个问题,当我想在创建页面之后将用户重定向到另一个不是索引页面的页面时,就会出现这个问题 我尝试在客户机文件夹中创建另一个视图,然后将“index”替换为“success”,例如新页面的名称 [HttpPost] public ActionResult Create(Client client) { if (ModelState.IsValid) { db.Clients.Add(client); db.SaveChanges();

我面临一个问题,当我想在创建页面之后将用户重定向到另一个不是索引页面的页面时,就会出现这个问题

我尝试在客户机文件夹中创建另一个视图,然后将“index”替换为“success”,例如新页面的名称

[HttpPost]
public ActionResult Create(Client client)
{
    if (ModelState.IsValid)
    {

        db.Clients.Add(client);
        db.SaveChanges();
        return RedirectToAction("Index"); // Here is the problem

    }
}
我还尝试了
重定向(“~/Client/Success”)
,但它也不起作用

谢谢你的帮助

这将起作用:

return RedirectToAction("Success","Client");
其中,
Success
是您的
操作名称
客户端
是您的
控制器名称

如果要重定向到同一控制器中的成功操作,则:

return RedirectToAction("Success");
在ClientController中,您的操作应如下所示:

public ActionResult Success()
{
  return View();
}
您正在使用:

return RedirectToAction("Index");

这会将您重定向到同一
控制器中的
索引
操作结果

您不仅需要在控制器中创建名为“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.
}
return RedirectToAction("Index");
return RedirectToAction("Success", "Client");
但是我不认为仅仅为了显示成功结果而使用重定向是个好主意。您最好在客户端文件夹中创建成功视图,就像您已经做的那样(假设您的控制器名为“ClientController”),然后返回视图结果而不是重定向:

[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.
}
看一看。你不是重定向到URL,而是重定向到一个操作。因此,这将重定向到当前控制器上的
索引
操作:

[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.
}
return RedirectToAction("Index");
return RedirectToAction("Success", "Client");
或者这将重定向到
客户端上的
成功
操作
控制器:

[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.
}
return RedirectToAction("Index");
return RedirectToAction("Success", "Client");

您可以尝试这样的Ajax重新请求:

 $('#myButton').click(function () {
            var url = '/MyControllerName/MyPage';
            var $Param = $('#SomeParam').val();     
            $.ajax({
                url: url,
                type: 'POST',
                cache: false,
                data: {
                   Param: $Param             
                }
            })

是的,我刚刚尝试过,但不起作用,资源找不到错误只是在客户端文件夹名称中创建一个视图它成功了我已经成功地编写了它,但我认为我的问题是我没有将方法success放在我的CLientcontroller中,这是我错误的原因吗?是的,在CLientcontroller中编写此代码。。。。public ActionResult Success(){return View();}是的,没问题!我完全忘了控制器,非常感谢!!