指定要在编辑操作中加载的视图
我正在开发我的第一个 ASP.NET MVC 3 应用程序,我有两个带视图的控制器,IceCreamController/IceCreamView 和 RecipeController/RecipeView,并且用户可以在每个控制器上进行选择并显示菜谱。
选择会导致显示一个 PartialView,其上有一个编辑链接。单击时,将显示该配方的 EditView,允许用户编辑所选配方项的属性。
伟大的。这工作得很好,除了目前 RecipeController 中的 POST 操作看起来像这样:
[HttpPost]
public ActionResult Edit(RecipeViewModel viewModel)
{
// updates the underlying model with the viewModel
// other things not germane to the discussion
return View();
}
并且最终总是显示 Recipe 的索引视图,这不是我想要的。相反,我希望能够在用户提交更改后将其发送回适当的视图(IceCreamView 或 RecipeView)。
我认为其他人也做过类似的事情。编辑完成后,如何传达应重定向到哪个控制器/操作?
注意: 我在上面添加了一些内容来澄清我有两个单独的控制器(IceCreamController 和 RecipeController),每个控制器都有一个视图,可以选择并最终执行 a
@Html.Partial("_Recipe", model.recipe)
以显示特定食谱的详细信息。我的问题是如何通过 RecipeController 上的编辑操作将页面重定向回 IceCreamView 或 RecipeView - 本质上,我如何传达它应该去的位置,因为食谱详细信息可能已通过任一路径显示。
采用的解决方案:
正如您可以在下面对 Darrin 的回答的评论中阅读的那样,由于我涉及多个控制器,解决方案是利用视图模型来传递控制器/操作,该控制器/操作应该编辑帖子操作完成后将重定向到以下内容。
由于我有多个这种情况的实例(通过多个路径到达编辑页面),我认为创建一个简单的 BaseViewModel 来保存此功能可能是有序的,然后让所有适用的视图模型继承自该 BaseViewModel。
我不认为它需要比这样的东西更多:
public BaseViewModel
{
public BaseViewModel(string controller, string action)
{
ControllerName = controller ?? string.empty;
ActionName = action ?? string.empty;
}
public string ControllerName { get; set; }
public string Action { get; set; }
}
然后可以修改视图模型的构造函数以传递控制器/操作并将其交给基类。
可能还有其他解决方案,如果是的话,我想听听它们。
I'm working on my first ASP.NET MVC 3 application and I've got two Controllers with Views, IceCreamController/IceCreamView and RecipeController/RecipeView, and on each the user can make a selection and display the recipe.
Selection causes a PartialView to be displayed which an Edit link on it. When clicked the EditView for this recipe is displayed, allowing the user to edit the attributes of the recipe item selected.
Great. This works fine except currently the POST action in the RecipeController looks like so:
[HttpPost]
public ActionResult Edit(RecipeViewModel viewModel)
{
// updates the underlying model with the viewModel
// other things not germane to the discussion
return View();
}
and that ends up always showing the Index view for Recipe, which isn't what I want. Rather, I'd like to be able to do is send the user back to the appropriate View (IceCreamView or RecipeView) when they've submitted their changes.
I assume that others have done something similar to this. How do you communicate which Controller/Action should be redirected to when the Edit is done?
Note:
I added a bit above to clarify that I've got two separate Controllers (IceCreamController and RecipeController) and each has a View that can select and ultimately do a
@Html.Partial("_Recipe", model.recipe)
to display the details of a particular recipe. My problem is how to get the page redirected back to either IceCreamView or RecipeView by the Edit Action on RecipeController - essentially, how do I communicate where it should go since the recipe details could have been displayed by either path.
Solution Employed:
As you can read below in the comments to Darrin's answer, since I've got more than a single controller involved, a solution is to utilize the viewmodel to pass in the controller/action that should be redirected to following when the Edit post action is completed.
As I've got more than a single instance of this situation (arriving at an Edit page via multiple paths), I think creating a simple BaseViewModel to hold this functionality might be in order and then have all the applicable viewmodels inherit from that BaseViewModel.
I'm don't think it needs to be anything more than something like:
public BaseViewModel
{
public BaseViewModel(string controller, string action)
{
ControllerName = controller ?? string.empty;
ActionName = action ?? string.empty;
}
public string ControllerName { get; set; }
public string Action { get; set; }
}
And then a viewmodel's constructor could just be modified to pass in the controller/action and hand that off to the base class.
There may be other solutions to this and if so, I'd like to hear them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者如果您想重定向,您可以有一个控制器操作来提供此视图,然后返回 RedirectToAction("IceCream"); 这可能比直接从 POST 操作返回视图更正确成功案例。
or if you wanted to redirect you could have a controller action that would serve this view and then
return RedirectToAction("IceCream");
which is probably more correct rather than directly returning a view from a POST action in case of success.