小编Joz*_*avý的帖子

ActionFilter的Order属性,从最低到最大,反之亦然?

我定义了两个ActionFilters:

[DefaultResources(Order = 2)]
[RenderTemplate(Order = 1)]
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是DefaultResources是在RenderTemplate之前执行的.但根据MSDN文档,它应该反之亦然:

[Filter1(Order = 2)]
[Filter2(Order = 3)]
[Filter3(Order = 1)]
public void Index()
{
    View("Index");
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,操作筛选器将按以下顺序执行:Filter3,Filter1,然后Filter2.

我正在使用.NET 4.并通过方法OnActionExecuted进行比较.我错过了什么吗?

asp.net-mvc action-filter order-of-execution

12
推荐指数
1
解决办法
3903
查看次数

具有OnComplete的Ajax.BeginForm始终更新页面

我在MVC中有简单的ajax形式.在AjaxOptions中,OnComplete设置为简单的javascript函数,它只做一件事 - 返回false.

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { UpdateTargetId = "DivFormId", HttpMethod = "Post", OnComplete = "preventUpdate" }))

function preventUpdate(xhr) {
    return false;       
}
Run Code Online (Sandbox Code Playgroud)

问题是,该页面已经更新.例如,在一个案例中,控制器在回发后返回部分视图,在其他情况下,它返回一些Json对象.我希望它在返回部分视图时更新页面,并在返回json时显示对话框窗口.不幸的是,当返回json时,它会清除页面(用json更新它),即使OnComplete函数返回false,如MSDN所说:要取消页面更新,请从JavaScript函数返回false.

如何根据收到的响应阻止页面更新?

谢谢!

-----更新-------

到目前为止,我找到了以下解 当我没有指定UpdateTargetId时,我可以手动执行我想要的响应.但是仍然没有记录返回false的行为.

ajax asp.net-mvc events ajaxform

11
推荐指数
1
解决办法
2万
查看次数

使用AttributeTargets.Class的自定义ValidationAttribute的客户端验证

是否可以对用于Class范围的自定义ValidationAttribute进行客户端验证?例如我的MaxLengthGlobal,它应确保所有输入字段的全局最大限制。

[AttributeUsage(AttributeTargets.Class)]
public class MaxLengthGlobalAttribute : ValidationAttribute, IClientValidatable
{
    public int MaximumLength
    {
        get;
        private set;
    }

    public MaxLengthGlobalAttribute(int maximumLength)
    {
        this.MaximumLength = maximumLength;
    }

    public override bool IsValid(object value)
    {
        var properties = TypeDescriptor.GetProperties(value);

        foreach (PropertyDescriptor property in properties)
        {
            var stringValue = property.GetValue(value) as string;

            if (stringValue != null && (stringValue.Length > this.MaximumLength))
            {
                return false;
            }
        }

        return true;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {       
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = this.FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType …
Run Code Online (Sandbox Code Playgroud)

c# model-view-controller class client-side-validation attributeusage

4
推荐指数
1
解决办法
2224
查看次数