ASP.NET MVC - 替代为[绑定(不包括=" ID和QUOT;) [英] ASP.NET MVC - Alternative for [Bind(Exclude = "Id")]

查看:155
本文介绍了ASP.NET MVC - 替代为[绑定(不包括=" ID和QUOT;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 [绑定(不包括=ID)] (<一个替代href=\"http://stackoverflow.com/questions/2142990/asp-mvc-the-id-field-is-required-validation-message-on-create-id-not-set-to\">Related问题)?

我可以写一个模型绑定?

Could I write a model binder?

推荐答案

是的,有:这就是所谓的视图模型。视图模型是其中是专门为一个给定的视图的特定需要的类。

Yes there is: it's called view models. View models are classes which are specifically tailored to the specific needs of a given view.

而不是:

public ActionResult Index([Bind(Exclude = "Id")] SomeDomainModel model)

使用:

public ActionResult Index(SomeViewModel viewModel)

其中视图模型仅包含需要被绑定的属性。然后,你可以在视图模型和模型之间的映射。这种映射可以与 AutoMapper

作为最佳实践,我会建议你总是使用视图模型和视图。

As best practice I would recommend you to always use view models to and from a view.

这篇关于ASP.NET MVC - 替代为[绑定(不包括=&QUOT; ID和QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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