从另一个 ActionResult 返回一个 ActionResult
假设我有以下代码,在记事本中模拟,所以请原谅任何小错误:)
//Default page
public ActionResult Index()
{
var musicViewModel
{
Albums = GetTopSellingAlbums(5),
Genres = GetTopGenres(5),
Artists = GetTopArtists(5)
};
return View(musicViewModel);
}
[HttpPost]
public ActionResult Index(MusicViewModel musicViewModel)
{
//For the example, pretend I have a class called musicStoreSubmission in my
//viewmodel which holds a few different fields the user fills out.
if(ModelState.IsValid)
{
//Do some actions based on the user submitting a form
}
//Else, refresh page with errors in Modelstate.
var musicViewModel
{
Albums = GetTopSellingAlbums(5),
Genres = GetTopGenres(5),
Artists = GetTopArtists(5)
};
return View(musicViewModel);
}
我关心的是,为了回发 ModelState 无效的任何错误,我需要再次生成视图模型,以便视图模型上的任何元素可以创建使用这些对象的页面(流派、艺术家等)。问题是它需要我将一些代码从 ActionResult 复制并粘贴到 ActionResult,这似乎使我的代码不太干燥。
有没有更好的方法来避免这样的重复代码?目前,我只是将视图模型所需的任何默认对象的生成移至单独的方法和/或构造函数中,但这有点混乱,因为我必须生成整个控制器可能需要的所有对象。我希望我能做的是将我的第二个索引操作指向第一个索引操作,并将其用作常规方法。我已经尝试了几种不同的方法来执行此操作,但似乎无法将 ActionResult 返回到另一个 ActionResult 中。
有什么想法吗?
Say I have the following code, mocked up in notepad, so excuse any minor errors :)
//Default page
public ActionResult Index()
{
var musicViewModel
{
Albums = GetTopSellingAlbums(5),
Genres = GetTopGenres(5),
Artists = GetTopArtists(5)
};
return View(musicViewModel);
}
[HttpPost]
public ActionResult Index(MusicViewModel musicViewModel)
{
//For the example, pretend I have a class called musicStoreSubmission in my
//viewmodel which holds a few different fields the user fills out.
if(ModelState.IsValid)
{
//Do some actions based on the user submitting a form
}
//Else, refresh page with errors in Modelstate.
var musicViewModel
{
Albums = GetTopSellingAlbums(5),
Genres = GetTopGenres(5),
Artists = GetTopArtists(5)
};
return View(musicViewModel);
}
My point of concern is that in order to postback any errors with the ModelState being invalid, I need to generate the viewmodel again so that any elements on the page that use those objects can be created (genres, artists etc.). The problem is it requires me copying and pasting some of the code from ActionResult to ActionResult, seemingly making my code not very DRY.
Is there a better way of avoiding repeated code like this? At the moment I have simply moved the generation of any default objects the viewmodel needs into a separate method and/or the constructor, but it's a bit messy since I have to generate all the objects I might need for the entire controller. What I was hoping I could do would be to point my second Index Action to the first Index Action and just use that as a regular method. I've tried a few different ways of doing this though and cant seem to return an ActionResult into another ActionResult.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议应用 Post/Redirect/Get 模式。它非常适合 MVC Web 应用程序。
检查此答案的代码示例:
ModelState.IsValid 或 Model.IsValid?
I would suggest applying Post/Redirect/Get pattern. It's perfectly suited for MVC web apps.
Check this answer for code sample:
ModelState.IsValid or Model.IsValid?
您可以返回另一个 ActionResult 方法,如下所示:
或者您可以将发布的模型传递回 ViewResult
第二种方法更好,因为您不需要重建 ViewModel
You can return an another ActionResult method like this:
or you could pass the posted model back to ViewResult
The second approach is better as you don't rebuild the ViewModel