你如何忽略/坚持MVC值时,您的视图模型不具备的许多领域为您的域模型? [英] How do you ignore/persist values in MVC when your view-model doesn't have as many fields as your domain model?

查看:147
本文介绍了你如何忽略/坚持MVC值时,您的视图模型不具备的许多领域为您的域模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用fluentNhibernate和Asp.net MVC,其中一个站点。我有一个编辑视图,允许用户编辑10属性的记录(对象)8。当您提交的形式和模型结合,两者不可编辑的字段回来视图模型为空字符串或默认DateTime值取决于属性的类型。

I have a site where I'm using fluentNhibernate and Asp.net MVC. I have an Edit view that allows user to edit 8 of the 10 properties for that record (object). When you submit the form and the Model binds, the two un-editable fields come back in the view-model as Empty strings or as default DateTime values depending on the type of property.

由于我还使用AutoMapper映射我的视图模型我的域实体,我不能只是从数据库中加载我的对象的新副本并手动设置2缺少的属性。最新最好的办法是坚持,我不想编辑这些领域?

Because I'm also using AutoMapper to map my view-model to my Domain Entity, I cannot just load a fresh copy of my object from the database and manually set the 2 missing properties. Whats the best way to persist those fields that I don't want edited?

这是确实可行的方法之一是坚持我在查看隐藏的输入字段中的值。这工作,但感觉恶心。我AP preciate任何建议。有没有在我的AutoMapper的方式来配置此需要的功能?

One way that does work is to persist the values in hidden Input fields on my View. That works but feels gross. I appreciate any recommendations. Is there a way in my AutoMapper to configure this desired functionality?

更新:
好了,我想我不是要忽视领域,我想确保我不会坚持null或空字符串值。忽略AutoMapper领域做到了这一点,它们都被忽略,并且是空的,当我试图映射之前保存到我的仓库。

UPDATE: Ok, So I guess I'm not trying to ignore the fields, I'm trying to make sure that I don't persist null or empty string values. Ignoring the fields in AutoMapper does just that, they get ignored and are null when I attempt to map them before Saved to my repository.

推荐答案

在asp.net mvc的DefaultModelBinder是可扩展的,并且可以重写它来创建自己的绑定模式。但是,这将涉及超过两个隐藏的输入域,这在我的角度来看,是不是总更多的工作。

The asp.net mvc DefaultModelBinder is extensible, and you can override it to create your own binding schema. But this will involve more work than two "hidden Input fields", which , in my point of view, is not that gross.

这篇关于你如何忽略/坚持MVC值时,您的视图模型不具备的许多领域为您的域模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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