MVC控制器操作-处理POST和GET时没有重复的代码 [英] MVC Controller Actions - Handle POST and GET with out duplicate code

查看:45
本文介绍了MVC控制器操作-处理POST和GET时没有重复的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发MVC 3 Razor应用程序,通常使用视图模型作为视图.

I have been working on this MVC 3 Razor app and typically utilize view models for my views.

相当多的视图模型包含更多信息,而不仅仅是与我在表单中进行交互的特定实体.因此,我的GET操作处理程序将初始化视图模型,并为每个属性提供预期的值等.

A fair number of my view models contain more information than just the particular entity that I am interacting with in my form. So my GET action handler will init the view model and provide each property with the intended value etc..

在我的POST操作处理程序中,我检查模型状态是否有效,如果不是,则重新显示带有错误的表单/视图.

In my POST action handler I check to see if the model state is valid, if not I redisplay the form/view with errors.

在我的POST操作处理程序中,我发现自己必须从GET操作处理程序中复制代码,以便再次重新呈现视图.如何实现我的控制器动作,这样我就不必复制负责收集视图模型数据的代码?

In my POST action handler I find myself having to copy the code from my GET action handler in order to re-render the view again. How can I implement my controller actions so that I am not having to copy the code that is responsible for gathering the data for the view model?

我尝试让动作处理程序同时处理POST和GET,但随后我要处理输入参数.我的POST操作处理程序将把视图模型作为输入参数,但对于GET操作处理程序则没有.

I have tried allowing my action handler to handle both POST and GET but then I have the input params to deal with. My POST action handler will have the view model as an input parameter but for the GET action handler will not.

推荐答案

在这种情况下,我们为视图模型创建了构建器.

In situations like these we create builders for our view models.

此信息.

这篇关于MVC控制器操作-处理POST和GET时没有重复的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆