我应该使用只有两个对象视图模型? [英] Should I use a view model for just two objects?

查看:96
本文介绍了我应该使用只有两个对象视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个接受对象的视图。

Lets say I have a view that accepts a Person object.

有三个属性,年龄

现在可以说,我添加一个文本字段,不是对象的一部分。

Now lets say I add another textbox field that's not part of the object.

我不需要文本框的值,它只是填充数据这对用户。

I don't need the value of the textbox, its just populated with data that's for the user.

在编辑领域和发布给控制器,让我们假设有一个验证的问题,所以你的回报与人对象的错误回

When you edit the fields and post the Person to the controller, lets assume there is a validation problem so you return the Person object back with Errors

问题现在是额外的文本框已经失去了它,因为它不是模型的一部分​​价值。

The problem is now the additional textbox has lost it's value since its not part of the model.

于是我做了一个视图模型使用字符串属性的字段和属性的人,以保持所有的值。好像会有一个更好的方式来保持价值的特殊文本框?

So I made a ViewModel with a string property for that field and a Person property to keep all the values. Seems like there would be a better way to keep the value in the "special" textbox?

推荐答案

您应该能够得到从发布的字段额外字段。你如何设置它第一次通过ViewBag?您应该能够再次设定。

You should be able to get that extra field from the posted fields. How do you set it first time, through the ViewBag? You should be able to set it again.

但究竟什么是错用视图模型?迟早你将有2个或3个额外的字段,或者一个人和一个约会。

But what exactly is wrong with using a ViewModel? Sooner or later you will have 2 or 3 extra fields, or a Person and an Appointment.

这篇关于我应该使用只有两个对象视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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