English 中文(简体)
B. 习俗验证局
原标题:Custom ValidationAttribute not firing IsValid function call in view model

我制定了自己的惯例<条码>。

public class UrlValidationAttribute : ValidationAttribute
{
    public UrlValidationAttribute() {}

    public override bool IsValid(object value)
    {
        if (value == null)
            return true;

        var text = value as string;
        Uri uri;

        return (!string.IsNullOrWhiteSpace(text) && 
                Uri.TryCreate(text, UriKind.Absolute, out uri));
    }
}

www.un.org/Depts/DGACM/index_spanish.htm 我是利用这一模式的,它完全发挥作用。 然而,现在我试图在一种观点模式上加以利用:

public class DeviceAttribute
{
    public DeviceAttribute(int id, attributeDefinition, String url)
    {
        ID = id;
        Url = url;
    }

    public int ID { get; set; }

    [UrlValidation]
    public String Url { get; set; }
}

部分观点也采用了这种观点模式:

@model List<ICMDB.Models.DeviceAttribute>

<table class="editor-table">

@foreach (var attribute in Model)
{
    <tr>

        @Html.HiddenFor(a => attribute.ID)

        <td class="editor-label">
            @Html.LabelFor(a => attribute.Url)
        </td>

        <td class="editor-field">
            @Html.TextBoxFor(a => attribute.Url)            
            @Html.ValidationMessageFor(a => attribute.Url)
        </td>

    </tr>
}

</table>

出于某种不了解的原因,虽然乌拉尔瓦利德加特大火的构造者,但IsValid功能却起火。 任何想法?

<><>Edit>: 在进一步调查方面,似乎正在出现这种情况,因为<代码>DeviceAttribute观点模式实际上是部分观点模式。 整页采用了一种不同的观点模式,其中包含<代码>DeviceAttribute浏览模型。 因此,当我的控制者行动被称作时,整页观模型已经形成,其价值已经填满,但没有建立<代码>DeviceAttribute的观测模型,因此没有进行验证。

最佳回答

I would recommend you using editor templates instead of writing foreach loops. I suppose that your main view model looks something like this:

public class MyViewModel
{
    public List<DeviceAttribute> Devices { get; set; }
    ...
}

您现在主要认为:

@model MyViewModel
@using (Html.BeginForm())
{
    <table class="editor-table">
        @Html.EditorFor(x => x.Devices)
    </table>
    <input type="submit" value="OK" />
}

以及相应的编辑模板(~/View/joint/EditorTemplates/DeviceAttribute.cshtml):

@model DeviceAttribute
<tr>
    @Html.HiddenFor(x => x.ID)

    <td class="editor-label">
        @Html.LabelFor(x => x.Url)
    </td>

    <td class="editor-field">
        @Html.TextBoxFor(x => x.Url)            
        @Html.ValidationMessageFor(x => x.Url)
    </td>
</tr>

而贵组织的行动则采用以下模式:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    ...
}

现在,缺省模式的约束者将成功地约束观点模式中的所有价值和验证。

http://lostechies.com/jimmybogard/09/07/building-forms-for-deep-view-model-graphs-in-asp-net-mvc/“rel=“nofollow”>nice blog post

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签