我有Controller一些行动,如下:
[HttpPost]
public ActionResult Create(CreateModel model)
{
if (model.SelectedCustomers.Count > 0 &&
model.SelectedVersions.Count > 0 &&
!string.IsNullOrWhiteSpace(model.ScriptName) &&
!string.IsNullOrWhiteSpace(model.ScriptText))
{
Script script;
...save to database...
return Edit(script.Id); //<---------Return other view here
}
else
{
...
}
}
[HttpGet]
public ActionResult Edit(int? scriptId)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
在以后Create的行动中运行,并节省了我的模型到数据库成功,我想给用户发送到Edit新创建的脚本视图.当我使用上面的代码时,特别是return Edit(script.Id);它只是将用户发送回Create视图而不是Edit视图.当用户Edit直接导航到操作时,或通过Html.ActionLink指向Edit一切正常的结果.
我究竟做错了什么?
这不符合你的想法:
return Edit(script.Id)
Run Code Online (Sandbox Code Playgroud)
它实际上并没有告诉框架去做那个动作.它只是返回该方法的返回值.在涉及ASP.NET MVC框架的任何组件之前,纯粹是一个C#问题.什么是回报值:
return View()
Run Code Online (Sandbox Code Playgroud)
所以,前者是真正的功能上是相同的事情,因为后者.每当你return View()在ASP.NET MVC中使用时,框架将通过检查当前被调用的动作来确定该视图,在本例中是这样Create.
你想要的不是返回Edit视图(即使你这样做,在这种情况下,用户仍然在Create URL上,这将导致混淆).你想要的是返回一个重定向告诉客户要请求的是下一个动作:
return RedirectToAction("Edit", new { scriptId = script.Id });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4909 次 |
| 最近记录: |