验证和编辑"可变" /可选类型在Asp.net MVC 3 [英] Validating and editing a "Changeable"/optional type in Asp.net MVC 3

查看:166
本文介绍了验证和编辑"可变" /可选类型在Asp.net MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个项目,我需要一种方法来指定属性是否已经改变。

for a project, I need a way to specify whether or not a property has changed.

因此​​,在一个模型,我想以下内容:

So in a model I would like the following:

public class Changeable<T>
{
   public bool Changed { get; set; }
   public T Value { get; set; }
}

public class MyModel
{
    [Range(1, 10)]
    public Changeable<int> SomeInt { get; set; }
}

查看:

@Html.EditorFor(model => model.SomeInt)

然后我会生成一个文本框(整型),并有一个复选框编辑器(改变)。
验证属性(范围等)应当复选框被选中,但被调用而不是当它被选中。

And then I would generate a editor with a textfield (the int) and a checkbox (is changed). The validation attributes (Range etc.) should be invoked when the checkbox is checked but not when it is unchecked.

我曾试图弥补上述用editortemplate为多变(然后是验证,模型绑定等),但我已经被editortemplate因为失去了它不能通用

I have tried to make the above with an editortemplate for Changeable (and then comes the validation, model binding etc.) but I'm already lost by the editortemplate because it can't be generic.

是我想可能的解决方案,或者是有其他reasonalble优雅的解决方案?

Is the solution I want possible, or is there another reasonalble elegant solution?

现在我正在开发与属性的String [] ChangedProperties 和大量的JavaScript来处理验证等一个解决方案,但它是相当丑陋,远远没有完成。

Right now I'm developing a solution with a property string[] ChangedProperties and a lot of Javascript to handle validation etc. but it's rather ugly and far from done.

谢谢...

推荐答案

您可以尝试使用动态类型的自定义范围验证属性:

You may try using dynamic types with a custom range validation attribute:

public interface IChangeable
{
    bool Changed { get; set; }
}

public class Changeable<T> : IChangeable
{
    public bool Changed { get; set; }
    public T Value { get; set; }
}

public class MyModel
{
    [MyRange(1, 10)]
    public Changeable<int> SomeInt { get; set; }
}

public class MyRangeAttribute : RangeAttribute
{
    public MyRangeAttribute(double minimum, double maximum): base(minimum, maximum)
    { }

    public MyRangeAttribute(int minimum, int maximum)
        : base(minimum, maximum)
    { }

    public override bool IsValid(object value)
    {
        var changeable = value as IChangeable;
        if (changeable == null || !changeable.Changed)
        {
            return true;
        }
        dynamic dynValue = value;

        return base.IsValid((object)dynValue.Value);
    }
}

然后控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyModel
        {
            SomeInt = new Changeable<int>
            {
                Changed = true,
                Value = 5
            }
        });
    }

    [HttpPost]
    public ActionResult Index(MyModel model)
    {
        return View(model);
    }
}

然后视图(〜/查看/主页/ Index.cshtml

@model MyModel

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.SomeInt)
    <button type="submit">OK</button>
}

和对应的编辑模板(注意编辑模板文件的名称)

and the corresponding editor template (notice the name of the editor template file)

~/Views/Shared/EditorTemplates/Changeable`1.cshtml


@model dynamic
@Html.CheckBox("Changed", (bool)Model.Changed)
@Html.Editor("Value")
@Html.ValidationMessage("")

这篇关于验证和编辑&QUOT;可变&QUOT; /可选类型在Asp.net MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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