复合视图模型对象造成远程验证失败 [英] Compound View Model object causing remote validation failure

查看:88
本文介绍了复合视图模型对象造成远程验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用复合视图模型的模式为少数人在这个项目的形式。它工作得很好。

I have used a pattern of compound view models for a few of the forms in this project. It works very well.

在这种情况下,我有一个VendorAddress视图模型。我用在少数地方的地址(ES)在这个项目所以我做了,而且我可以重新使用地址视图模型对象。然后,我desided我想要做的状态codeS和拉链等等的非常彻底的检查,我desided我会尝试在数据库中使用远程验证和检查状态和zip code aganst一个存储组表

In this case I have a VendorAddress view model. I use Address(es) in a few places in this project so I made and Address view model object that I can re-use. Then I desided I want to do a very thorough checking of the state codes and zips so, I desided I would try to use remote validation and check the state and zip code aganst a stored set of tables in the database.

问题是我没有得到值回了我所要求的(在这种情况下,国家和邮​​政code)等领域的远程操作方法。我相信这是因为该名称mangleing的MVC框架确实为化合物或子类型ID(S),在这种情况下,像ADDRESS_LINE1'和'ADDRESS_STATE'和'Address_Postal code'做名字的投入

The problem is I am not getting values back to the remote action methods for the fields I'm asking for (in this case State and PostalCode). I believe this is because of the name mangleing that the MVC framework does for compound or sub types for id(s) for the inputs in this case it is making names like 'Address_Line1' and 'Address_State' and 'Address_PostalCode'.

最后我的问题是,有没有办法从基本视图模型对象关闭pre-挂起地址_'的一种方式,MVC框架能的疗法后门柱把它窥知到对象表格?

Ultimately the question I have is, is there a way of turning off the pre-pended 'Address_' from the base view model object in a way that MVC framework can put it bak into the object after ther post of the form?

    public class AddressViewModel
{
    [ScaffoldColumn(false)]
    public int AddressId { get; set; }
    [Required(ErrorMessage = "Please enter the first address line")]
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    [Required(ErrorMessage = "Please enter a city name")]
    public string City { get; set; }
    [Required(ErrorMessage = "Please enter a state 2 letter code")]
    [StringLength(2)]
    [Remote("CheckState", "Validation", AdditionalFields = "PostalCode")]
    public string State { get; set; }
    [Required(ErrorMessage = "Please enter a postal code")]
    [Remote("CheckZip", "Validation", AdditionalFields = "State")]
    [Display(Name = "Zip / Postal Code")]
    public string PostalCode { get; set; }
    public string Country { get; set; }
}

public class VendorContactViewModel
{
    public int VedorContactId { get; set; }
    public int ContactVendorId { get; set; }
    public int ContactId { get; set; }
    [Required]
    [Display(Name = "Contact Type")]
    public byte ContactTypeId { get; set; }
    public string ContactType { get; set; }
    [Required]
    [Display(Name = "Contact Info")]
    public string ContactInfo { get; set; }
    [Display(Name = "Contact Label")]
    public string ContactLabel { get; set; }
    public IEnumerable<SelectListItem> ContactTypes { get; set; }
}

然后在表单渲染后,它看起来是这样的:

then in the form after rendering it looks like this:

    <input type="text" value="" size="2" name="Address.State" maxlength="2" id="Address_State" 
  data-val-required="Please enter a state 2 letter code" data-val-remote-url="/Validation/CheckState" 
  data-val-remote-additionalfields="*.State,*.PostalCode" data-val-remote="&amp;#39;State&amp;#39; is invalid." 
  data-val-length-max="2" data-val-length="The field State must be a string with a maximum length of 2." 
  data-val="true" class="input-validation-error">

我得到的远程呼叫,但没有什么参数的国家和邮政code,所以不显眼的程序被解雇,但我认为它不知道到哪里寻找数据,即使我有键入mensioned领域的东西。正如你可以在渲染code中的ID =ADDRESS_STATE和数据VAL-远程additionalfields =见的 .STATE,的.Postal code

I get the remote call but there is nothing in the parameters "State" and "PostalCode", so the unobtrusive routines are firing but I think it does not know where to look for the data even though I have type something in the mensioned fields. As you can see in the rendered code the id="Address_State" and the data-val-remote-additionalfields=".State,.PostalCode"

感谢您的时间和精力,

欧文

推荐答案

尝试指定preFIX帮助模型绑定正确绑定 Address.Postal code Address.State 请求值到其对应的动作参数:

Try specifying a prefix to help the model binder correctly bind the Address.PostalCode and Address.State request values to their corresponding action parameters:

public ActionResult CheckState(
    [Bind(Prefix = "Address.State")]string State, 
    [Bind(Prefix = "Address.PostalCode")]string PostalCode
)
{
    ...
}

这篇关于复合视图模型对象造成远程验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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