为什么我的ActionFilterAttribute没有在带有ajax请求的帖子之前执行?

时间:2014-03-12 16:16:45

标签: jquery ajax asp.net-mvc-4 entity-framework-5 action

我尝试在我的控制器上使用重写的OnActionExecute来使用jQuery.post在post中接收的模型中插入其他值,但是在这种情况下,在我的" Save"之前没有调用OnActionExecute。行动。在POST操作之前我是否可以使用我的模型进行搜索?

这是我的OnActionExecuting(示例):

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (filterContext.ActionParameters.ContainsKey("model") || filterContext.ActionParameters.ContainsKey("Model"))
    {
        var result = filterContext.ActionParameters["model"] as Apoio;
        if (result != null) result.StringProperty = "value of string property";
    }
}

这对我来说是必要的,因为我的模型有关系1:N(跟随): 动作:

public ActionResult Save(Model mymodel)
{
    // implementation
}

型号:

public class Model
{
    public int Id { get; set; }
    public string StringPropert { get; set; }
    private ICollection<ModelChild> _listChild;
    public ICollection<ModelChild> ListChild { 
        get
            {
                if (!string.IsNullOrWhiteSpace(StringProperties))
                {
                    var modelChild = new ModelChild(StringProperty);
        var list = (from item in model.GetAllChilds(StringProperty).Where(item => item .MasterId == this.Id) select item).ToList();
                    return list;
                }
                return _listChild;
            } 
    }
}


public class ModelChild
{
    public int Id { get; set; }
    public string StringPropert { get; set; }
    public int MasterId { get; set; } // <== reference to Model.Id
}

有人有想法吗? 感谢

0 个答案:

没有答案