ASP.NET MVC与远程验证绑定 [英] ASP.NET MVC Binding with Remote Validation

查看:251
本文介绍了ASP.NET MVC与远程验证绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型 MODELA 与成员 toBeRemoteChecked 和模型 MapToA 与成员 valueToMap 。每当我创建 MODELA 的一个实例,我还需要 MapToA 的一个实例,所以我有一个模型 CreateModelA ,其中包括一名成员 MODELA 和成员 valueToMap 。当提交表单时,我添加了 MODELA 到数据库表 MODELA 创建和实例添加到 MapToA 其中包括 MODELA valueToMap 的身份证。在条款的代码

I have a model ModelA with a member toBeRemoteChecked and a model MapToA with a member valueToMap. Whenever I create an instance of ModelA, I also need an instance of MapToA, so I have a model CreateModelA which includes a member modelA and a member valueToMap. When the form is submitted, I add the modelA to the database table ModelA and create and add an instance to MapToA which consists of an id of modelA and the valueToMap. In Terms of code

public class ModelA
{
    [Key]
    public int ID { get; set; }
    [Required, Remote("isValid", "MyController", ErrorMessage = "not valid")]
    public string toBeRemoteChecked { get; set; }
}

public class MapToA
{
    [Key]
    public int Map_ID { get; set; }
    [Required]
    public int modelAID { get; set; }
    [Required]
    public int valueToMap { get; set; }
}

public class CreateModelA
{
    public ModelA modelA { get; set; };
    public int valueToMap { get; set; };
}

当我修改 MODELA的实例,值 MapToA 并不重要(在大多数情况下有 mapToA 具有相同 MODELA ID),但 toBeRemoteChecked 仍然是重要的远程验证。

When I edit an instance of ModelA, values in MapToA don't matter (and in most cases there's more than one instance of mapToA with the same modelA id), but the remote validation of toBeRemoteChecked remains important.

我的问题:为验证方法绑定:

My Problem: binding for the validation method:

public ActionResult isValid(string toBeRemoteChecked) { ... }

如果我离开它,因为它是,它正在编辑一个 MODELA ,而不是当我创建一个 MODELA 通过 CreateModelA (我一直得到空值 toBeRemoteChecked )。当我使用BindPrefix

If I leave it as it is, it is working when editing a ModelA, but not when I'm creating a ModelA via CreateModelA (I always get null value in toBeRemoteChecked). When I use the BindPrefix

public ActionResult isValid([Bind(Prefix = "modelA.toBeRemoteChecked")] string toBeRemoteChecked) { ... }

当我创建它正在开发一种 MODELA ,而不是当我编辑它。

it is working when I create a ModelA, but not when I'm editing it.

当我尝试通过增加一个 ... @Name =toBeRemoteChecked... (而不是 modelA.toBeRemoteChecked 这是由HTML帮助创建)在htmlAttributes的 @ Html.TextBoxFor ,然后验证工作,但价值表中获取的失落的结合,我得到的错误,当值保存到数据库中(空值)。

When I try to change the "name" in the Create.cshtml by adding a ... @Name = "toBeRemoteChecked" ... (instead of the modelA.toBeRemoteChecked that's created by the HTML helper) in the htmlAttributes of the @Html.TextBoxFor, then validation is working, but the binding of the value to the table get's lost and I get the error when the values are saved to the database (null value).

所以,我该如何实现不同的结合创建和编辑?

So, how do I achieve the different binding for creating and editing?

到目前为止,我的解决方法是让 MODELA CreateModelA :IValidatableObject 并检查会员 toBeRemoteChecked 在我的公开的IEnumerable<&为ValidationResult GT;验证(ValidationContext validationContext)方法。但是,一个显示在窗体的顶部,而不是在TextFor框的位置错误信息

So far, my workaround is to make ModelA and CreateModelA : IValidatableObject and check the member toBeRemoteChecked in my public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) method. But that one displays the error messages on top of the form and not at the place of the TextFor box.

所以:最好的解决方案:如何做远程绑定?验证在这两种情况下工作。

So: best solution: how to do the binding that the remote validation works in both cases?

其次最好的:如何在物体附近显示 IValidatableObject 的错误消息,在那里属于(右手头得到的错误讯息,而不是提交后)

Second best: how to display the error messages of IValidatableObject near the object where it belongs to (and get the error messages right at hand, not after submitting)

不同的思路或解决方案:欢迎

Different ideas or solutions: welcome.

感谢。

推荐答案

我发现没有继承认为,只要稍微解决了我的绑定问题的解决方案(无需视图模型)改变我的代码。

I found a solution without inheritance (and without view models) that solves my binding problem with just little change to my code.

有进行远程验证结合两种方式,你可以只通过一个必须远程检查成员

There's two ways of binding for remote validation, you can either just pass the member that has to be remote checked

public ActionResult isValid(string toBeRemoteChecked) { ... }

也可以通过类成员的实例。

or you can pass the instance of the class of that member.

public ActionResult isValid(ModelA modelA) { ... }

在第二个变型,当然,你要替换 toBeRemoteChecked modelA.toBeRemoteChecked 。在这第二个版本在两种情况下,结合工程 - 编辑,并在上下文创造我上面的 MODELA 的实例时的时候。为了使绑定工作,这是至关重要的,远程验证方法的参数名称中的 CreateModelA ,即 MODELA 在我的情况。

Inside the second variant, of course, you have to replace toBeRemoteChecked with modelA.toBeRemoteChecked. On this second version the binding works in both cases - when editing and also when creating my instance of ModelA in the context above. In order to make the binding work, it's crucial that the parameter name of the remote validation method matches the member name in the CreateModelA, i.e. modelA in my case.

如果你有一个非常复杂的模型,你可以初始化参数 MODELA 与您希望通过使用绑定使用会员/包括,也就是说,在我来说,我会使用

In case you have a very complex model, you can just initialize the parameter modelA with the members you want to use by using bind/include, i.e. in my case I'd use

public ActionResult isValid([Bind(Include = "toBeRemoteChecked")] ModelA modelA) { ... }

通过默认情况下(不包括),所有其他成员将继续为空或有一个默认值 - 所以你需要使用仅包括如果需要其他成员进行验证,以及 - 在我的情况,我会忽略当包含)相同

By default (without Include), all other members will remain null or have a default value - so you need to use Include only if you need other members for validation as well - in my case, I would have the same when omitting the Include)

这篇关于ASP.NET MVC与远程验证绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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