访问编辑器模板中的远程验证属性(MVC 5)

时间:2023-01-24 22:15:22

I have a basic model which I'm trying to do some Remote validation using the remote attribute.

我有一个基本模型,我正在尝试使用Remote属性进行一些远程验证。

    [Remote("IsCodeValid", "Utilities", ErrorMessage = "Code already in use")]
    [Required]
    public string Code { get; set; }

If I do a normal Html.Editorfor(m => m.Code) then everything works fine and I am able to successfully use the Remote Validation. So the attribute and the controller method in a normal use case is working perfectly.

如果我做一个普通的Html。然后一切工作正常,我能够成功地使用远程验证。因此,在正常的用例中,属性和控制器方法工作得很好。

The issue

这个问题

I'm trying to use an EditorTemplate in which I'm using a 3rd party library to re-create the Input control. In the EditorTemplate I need to either access the Remote validation attributes such as all the "data-val" attributes and then add them on or if there is some method available that I can get a list of them and then just add them on in one go.

我正在尝试使用一个编辑器模板,在其中我使用第三方库重新创建输入控件。在EditorTemplate中,我需要访问诸如所有“data-val”属性之类的远程验证属性,然后添加它们,或者如果有某种方法可以获得它们的列表,然后一次性添加它们。

I have managed to get access to the Html.ViewData within the editor template.

我设法访问了Html。编辑器模板中的ViewData。

Can someone point me in the right direction? Has someone done something like this before?

有人能给我指出正确的方向吗?以前有人做过这样的事吗?

Here is the flow of my Editor template so it gives you an idea of what I'm trying to achieve.

这是我的编辑器模板的流程,它可以让您了解我要实现的目标。

// Start of editor template
@model String

@{
var htmlAttributes = new Dictionary<string, object>();

// Observe how I can manually add attributes to the dictionary
htmlAttributes.Add("data-val", "true");

// Able to add Css class from ViewModel
if (Html.ViewData.ContainsKey("class"))
{
    var cssClass = Html.ViewData.SingleOrDefault(kvp => kvp.Key.ToLower() == "class");
    htmlAttributes.Add(cssClass.Key, cssClass.Value);
}
}
@(

// At this point is where I need to pass the htmlAttributes with the [Remote] validation values so that it can be added to the input control
Html.Custom3rdPartyLibrary()
    .TextEditorFor(x => Model)
    .HtmlAttributes(htmlAttributes)
    .Render()
)

1 个解决方案

#1


1  

You can use the GetUnobtrusiveValidationAttributes() method of the HtmlHelper to get the validation attributes based on the ModelMetadata.

您可以使用HtmlHelper的GetUnobtrusiveValidationAttributes()方法获得基于模型元数据的验证属性。

@model string
@{
    // Get the ModelMetadata
    ModelMetadata metadata = ViewContext.ViewData.ModelMetadata;
    // Get the validation attributes
    IDictionary<string, object> attributes = @Html.GetUnobtrusiveValidationAttributes(metadata.PropertyName, metadata );
}

based on the validation attributes applied to you property, attributes will contain the following key/value pairs

根据应用于属性的验证属性,属性将包含以下键/值对

data-val-required: "The Code field is required."
data-val-remote: "Code already in use"
data-val-remote-url: "/Utilities/IsCodeValid"
data-val-remote-additionalfields: "*.Code"
data-val: true

and then you can add any further attributes such as class names to the dictionary and pass it to your custom helper method using .HtmlAttributes(attributes)

然后可以向字典中添加任何属性,如类名,并使用。htmlattributes(属性)将其传递给自定义helper方法

#1


1  

You can use the GetUnobtrusiveValidationAttributes() method of the HtmlHelper to get the validation attributes based on the ModelMetadata.

您可以使用HtmlHelper的GetUnobtrusiveValidationAttributes()方法获得基于模型元数据的验证属性。

@model string
@{
    // Get the ModelMetadata
    ModelMetadata metadata = ViewContext.ViewData.ModelMetadata;
    // Get the validation attributes
    IDictionary<string, object> attributes = @Html.GetUnobtrusiveValidationAttributes(metadata.PropertyName, metadata );
}

based on the validation attributes applied to you property, attributes will contain the following key/value pairs

根据应用于属性的验证属性,属性将包含以下键/值对

data-val-required: "The Code field is required."
data-val-remote: "Code already in use"
data-val-remote-url: "/Utilities/IsCodeValid"
data-val-remote-additionalfields: "*.Code"
data-val: true

and then you can add any further attributes such as class names to the dictionary and pass it to your custom helper method using .HtmlAttributes(attributes)

然后可以向字典中添加任何属性,如类名,并使用。htmlattributes(属性)将其传递给自定义helper方法