如何调用的UpdateModel()当您排除绑定属性? [英] How do you exclude properties from binding when calling UpdateModel()?

查看:187
本文介绍了如何调用的UpdateModel()当您排除绑定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送到我的控制器的编辑操作视图模型。该视图模型包含EntityObjects引用。 (是啊,我很好与它并不需要希望复制在视图模型的所有实体属性)。

I have a view model sent to the edit action of my controller. The ViewModel contains references to EntityObjects. (yea i'm fine with it and don't need to want to duplicate all the entities properties in the viewmodel).

我实例化视图模型,然后调用的UpdateModel。我得到一个错误,一个属性为空这是很好的,因为它是一个相关的模型。我想从模型绑定过程中被排除约束的财产。调试它,我看到在模型绑定正在试图将该属性设置为空值的实体。

I instantiate the view model and then call UpdateModel. I get an error that a property is "null" which is fine since it is a related model. I am trying to exclude the property from being bound during model binding. On debugging it I see in the entity where the model binder is trying to set the value of the property to null.

下面是我的编辑操作:

var model = new SimplifiedCompanyViewModel(id);

var excludeProperties = new string[] { 
   "Entity.RetainedEarningsAccount.AccountNo"
   ,"Property.DiscountEarnedAccount.ExpenseCodeValue"
   ,"Entity.EntityAlternate.EntityID"
   ,"Property.BankAccount.BankAccountID"
   ,"Entity.PLSummaryAccount.AccountNo"
   ,"Property.RefundBank.BankAccountID"
   ,"Company.Transmitter.TCC"
};

try
{
    UpdateModel<SimplifiedCompanyViewModel>(model, String.Empty, null, excludeProperties);

    if (ModelState.IsValid)
    {
       //db.SaveChanges();
    }
       return RedirectToAction("Index");
}
catch
{
    return View(model);
}

我已经看过有关指定preFIX一些其他问题,但我不认为那是因为我告诉它绑定到视图模型的实例不仅仅是实体对象的问题。

I have looked at a few other issues about specifying a "prefix" but I don't think that is the issue since I am telling it to bind to the viewmodel instance not just the entity object.

我是否正确不包括的属性?奇怪的是仅仅是似乎对这个项目的情况发生。我怀疑它可能是与事实,实际上是有我的相关实体不予退款银行的问题。但我不存在,并没有看到同样的问题等相关物品。

Am I excluding the properties correctly? Strange thing is is only seems to happen on this item. I suspect it may be an issue with the fact that there is actually no refund bank related to my entity. But I have other related items that don't exist and don't see the same issue.

更多信息...因为我告诉我的模型没有设计好。

More info... since I'm told me model isn't designed well.

本公司相关的的BankAccount。本公司视图显示当前相关BankAccount.BankAccountId并没有与BankAccount.Key隐藏字段。我使用jQueryUI的自动完成功能提供的银行账户显示BankAccount.BankAccountId一个下拉,并在选中一个jQuery的code改变隐藏字段有正确的密钥值。所以,当这贴我不希望当前bankaccounts BankAccountID修改,所以我希望它跳过绑定字段。

The Company is related to a BankAccount. The Company view shows the currently related BankAccount.BankAccountId and there is a hidden field with the BankAccount.Key. I use jQueryUI autocomplete feature to provide a dropdown of bank account displaying the BankAccount.BankAccountId and when one is selected the jQuery code changes the hidden field to have the correct Key value. So, when this is posted I don't want the current bankaccounts BankAccountID modified, hence I want it to skip binding that field.

如果我在模型中排除BankAccountId再上的BankAccount编辑视图,用户将永远无法改变BankAccountId因为它不会被绑定。我不知道怎么这表明一个贫穷的模型设计。

If I exclude BankAccountId in the model then on the BankAccount edit view the user would never be able to change the BankAccountId since it won't be bound. I'm not sure how this indicates a poor model design.

推荐答案

另一个选择是根本不包括在你看来这个属性,它不会被绑定。是的 - 你仍然是开放的,如果有人创建它的网页上,然后注射模型,但它是另一种选择。 MVC中的默认模板将创建EditorFor等作为单独的项目,这样你可以删除它们。这prevents您使用与EditorForModel单行视图编辑器,但模板不适合你反正产生这种方式。

Another option here is simply don't include this attribute in your view and it won't be bound. Yes - you are still open to model injection then if someone creates it on the page but it is another alternative. The default templates in MVC will create your EditorFor, etc as separate items so you can just remove them. This prevents you from using a single line view editor with EditorForModel, but the templates don't generate it that way for you anyways.

编辑(添加上述评论)

DRY一般适用于逻辑,不查看模型。一种观点=一个视图模式。使用automapper可以轻松地在它们之间进行映射。吉米·博加德对这个伟大的属性,使得它几乎是自动的 - 即在创建视图模型,加载您的客户的实体,例如,在操作方法返回。然后AutpMap属性将其转换为一个视图模型。请参见 lostechies.com/jimmybogard/2009/06 / 30 /如何-WE-DO-MV​​C-视图模型

DRY generally applies to logic, not to view models. One view = one view model. Use automapper to easily map between them. Jimmy Bogard has a great attribute for this that makes it almost automatic - ie you create the view model, load up your Customer entity for example, and return it in the action method. The AutpMap attribute will then convert it to a ViewModel. See lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models

这篇关于如何调用的UpdateModel()当您排除绑定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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