OnActionExecuting 在开始操作之前添加到模型

发布于 2025-01-04 01:36:37 字数 906 浏览 0 评论 0原文

我有以下内容:

 public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        var model = filterContext.Controller.ViewData.Model as BaseViewModel;

        if (model == null)
        {
            model = new BaseViewModel();
            filterContext.Controller.ViewData.Model = model;
        }

        model.User = (UserPrincipal)filterContext.HttpContext.User;
        model.Scheme = GetScheme();
    }

现在逐步执行,我可以看到模型上的用户和方案正在填充。

当我采取行动时,它们都为空?

我在这里做错了什么?

除此之外,这是添加到模型的正确方法吗?

这是控制器代码:

[InjectStandardReportInputModel]
public ActionResult Header(BaseViewModel model)
{
    //by this point model.Scheme is null!!

}

I have the following:

 public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        var model = filterContext.Controller.ViewData.Model as BaseViewModel;

        if (model == null)
        {
            model = new BaseViewModel();
            filterContext.Controller.ViewData.Model = model;
        }

        model.User = (UserPrincipal)filterContext.HttpContext.User;
        model.Scheme = GetScheme();
    }

Now stepping through that i can see the user and scheme on the model are being populated.

By the time I get to the action however they are both null?

What am i doing wrong here?

And adding to this, is this the proper way of adding to the model?

Here is the controller code:

[InjectStandardReportInputModel]
public ActionResult Header(BaseViewModel model)
{
    //by this point model.Scheme is null!!

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一向站在原地 2025-01-11 01:36:37

Controller.ViewData.Model 未填充 asp.net mvc 中的操作参数。该属性用于将数据从操作传递到视图。

如果由于某种原因您不想使用自定义 Model Binder(这是在 asp.net-mvc 中填充操作参数的标准推荐方法),您可以 ActionExecutingContext.ActionParameters 属性

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.ActionParameters["model"] = new BaseViewModel();
        // etc
    }

Controller.ViewData.Model is not populating action parameters in asp.net mvc. That property is used to pass data from action to view.

If for some reason you do not want to use custom Model Binder (which is the standard, reccommended way of populating action parameters in asp.net-mvc), you could you ActionExecutingContext.ActionParameters Property

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.ActionParameters["model"] = new BaseViewModel();
        // etc
    }
千秋岁 2025-01-11 01:36:37

回复有点晚了,但对其他人很有用。我们可以通过稍微装饰一下我们的属性来获取 OnActionExecuting 中 model 的值。

这是我们的过滤器类别

public sealed class TEST: ActionFilterAttribute
{

   /// <summary>
    /// Model variable getting passed into action method
    /// </summary>
    public string ModelName { get; set; }

    /// <summary>
    /// Empty Contructor
    /// </summary>
    public TEST()
    {
    }

    /// <summary>
    /// This is to get the model value by variable name passsed in Action method
    /// </summary>
    /// <param name="modelName">Model variable getting passed into action method</param>
    public TEST(string modelName)
    {
        this.ModelName = modelName;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var result = filterContext.ActionParameters.SingleOrDefault(ap => ap.Key.ToLower() == ModelName.ToString()).Value;
    }

}

    THIS IS OUR ACTION METHOD PLEASE NOTE model variable has to be same
    [HttpPost]
    [TEST(ModelName = "model")]
    public ActionResult TESTACTION(TESTMODEL model)
    {
    }

,仅此而已......如果您喜欢答案,请投票

Bit late for the reply but it will be useful for others. we can get the value of model in OnActionExecuting by just decoration our attribute a little more.

THIS IS OUR FILTER CLASS

public sealed class TEST: ActionFilterAttribute
{

   /// <summary>
    /// Model variable getting passed into action method
    /// </summary>
    public string ModelName { get; set; }

    /// <summary>
    /// Empty Contructor
    /// </summary>
    public TEST()
    {
    }

    /// <summary>
    /// This is to get the model value by variable name passsed in Action method
    /// </summary>
    /// <param name="modelName">Model variable getting passed into action method</param>
    public TEST(string modelName)
    {
        this.ModelName = modelName;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var result = filterContext.ActionParameters.SingleOrDefault(ap => ap.Key.ToLower() == ModelName.ToString()).Value;
    }

}

    THIS IS OUR ACTION METHOD PLEASE NOTE model variable has to be same
    [HttpPost]
    [TEST(ModelName = "model")]
    public ActionResult TESTACTION(TESTMODEL model)
    {
    }

AND THATS IT.....PLEASE VOTE IF YOU LIKE THE ANSWER

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文