mvc 2.0 updatemodel 和我的 ID 列

发布于 2024-08-25 02:32:17 字数 327 浏览 4 评论 0原文

我在 MVC 2.0 应用程序中创建了一个创建视图,默认情况下它包含一个整数 ID 列字段。

这绝对是我不需要的领域。

如果我在尝试在代码中创建对象时删除该字段并使用 updatemodel,是否会出现某些问题,因为它看不到我的 ID 列数据被传入,即使它是自动递增的?

另外,我注意到在 NerdDinner 示例中,使用了 updatemodel,然后调用了repository.save 方法。

我认为 updatemodel 会将对象保存到数据库中。为什么然后调用 .save 方法?或者我错过了什么?

任何对此的帮助将不胜感激。

干杯

I have created a create view within my MVC 2.0 Application and by default it included a field for the integer ID Column.

This is definitely a field i do not need.

If i remove the field and use updatemodel when trying to create the object in code, will something break because it doesnt see my ID column data being passed in, even though it is auto increment?

Also, i noticed that in the NerdDinner example, updatemodel was used and after that the repository.save method was called.

I thought that updatemodel would save the object to the database..why then call the .save method afterwards? Or have i missed something?

Any help with this would be appreciated.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-09-01 02:32:17

据我了解,UpdateModel方法将清除对象中的所有数据。这是因为 MVC 是基于往返的。如果您需要保持状态,任何出去的东西都应该回来。有关更多详细信息,请参阅此问题

在我看来,处理这种情况的更好方法是将输入模型类作为操作参数,将其传递给服务调用以更新数据库中的域实体。 (或者,如果您确实需要,此映射代码也可以位于操作方法中。)

另请注意直接绑定到数据库模型可能引入的安全漏洞。

As I understand it, the UpdateModel method will blow away all data in the object. This is because MVC is round-trip based. Anything that goes out should come back if you need to keep state. See this question for more details.

A better way to handle this scenario in my opinion is to have an input model class as an Action parameter which is passed to a service call to update the domain entity in the DB. (Or this mapping code could be right in the action method if you really want.)

Also please be aware of the security vulnerabilities that could be introduced by binding directly to your DB model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文