ASP.NET MVC - HybridViewResult (ViewResult /PartialViewResult)

发布于 2024-09-19 08:37:35 字数 279 浏览 7 评论 0原文

是否可以构建一个混合 ViewResult,根据 AjaxRequest 或 HttpRequest 返回 PartialViewResultViewResult

IsAjaxRequest -->返回部分视图结果 !IsAjaxRequest --> return ViewResult

据我所知,我的 HybridViewResult 应该从 ViewResultBase 派生。

但是FindView方法如何实现呢?

Is it possible to build a hybrid ViewResult that returns in depedency of an AjaxRequest or HttpRequest a PartialViewResult or ViewResult?

IsAjaxRequest --> return PartialViewResult
!IsAjaxRequest --> return ViewResult

As far as I know my HybridViewResult should derive from ViewResultBase.

But how to implement the FindView method?

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

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

发布评论

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

评论(4

梦一生花开无言 2024-09-26 08:37:35

尝试:

public class HybridViewResult : ActionResult
{
    public string ViewName { get; set; }
    public HybridViewResult () { }
    public HybridViewResult (string viewName ) { this.ViewName = viewName ; }
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null) throw new ArgumentNullException("context");
        var usePartial = ShouldUsePartial();
        ActionResult res = GetInnerViewResult(usePartial);
        res.ExecuteResult(context);
    }
    private ActionResult GetInnerViewResult(bool usePartial)
    {
        var view = ViewName;
        ActionResult res;
        if(String.IsNullOrEmpty(view)) {
             res = usePartial ? new PartialViewResult(view) : new ViewResult(view);
        }
        else {
             res = usePartial ? new PartialViewResult() : new ViewResult();
        }
        return res;
    }
    private bool ShouldUsePartial(ControllerContext context) {
        return false; //your code that checks if you need to use partial here
    }
}

添加任何构造函数 &根据需要获取InnerViewResult变量,即传递模型。

Try:

public class HybridViewResult : ActionResult
{
    public string ViewName { get; set; }
    public HybridViewResult () { }
    public HybridViewResult (string viewName ) { this.ViewName = viewName ; }
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null) throw new ArgumentNullException("context");
        var usePartial = ShouldUsePartial();
        ActionResult res = GetInnerViewResult(usePartial);
        res.ExecuteResult(context);
    }
    private ActionResult GetInnerViewResult(bool usePartial)
    {
        var view = ViewName;
        ActionResult res;
        if(String.IsNullOrEmpty(view)) {
             res = usePartial ? new PartialViewResult(view) : new ViewResult(view);
        }
        else {
             res = usePartial ? new PartialViewResult() : new ViewResult();
        }
        return res;
    }
    private bool ShouldUsePartial(ControllerContext context) {
        return false; //your code that checks if you need to use partial here
    }
}

Add any constructor & GetInnerViewResult variations as needed i.e. to pass Model.

緦唸λ蓇 2024-09-26 08:37:35

这是对 eglasius 答案的稍微精简的看法。我实际上正在解决类似的问题,只是我需要返回 JsonResult。

(未经测试的)NormalOrAjaxResult 只是让您为非 ajax 请求指定一个操作结果,为 ajax 请求指定一个操作结果。因为这些是 ActionResults,所以您可以混合重定向、视图、部分和 Json 视图结果。

public class NormalOrAjaxResult : ActionResult
{
    private readonly ActionResult _nonAjaxActionResult;
    private readonly ActionResult _ajaxActionResult;

    public NormalOrAjaxResult(ActionResult nonAjaxActionResult, ActionResult ajaxActionResult)
    {
        _nonAjaxActionResult = nonAjaxActionResult;
        _ajaxActionResult = ajaxActionResult;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var isAjaxRequest = context.HttpContext.Request["isAjax"];
        if (isAjaxRequest != null && isAjaxRequest.ToLower() == "true")
        {
            _ajaxActionResult.ExecuteResult(context);    
        } else
        {
            _nonAjaxActionResult.ExecuteResult(context);
        }
    }
}

This is a slightly more stripped down take on eglasius's answer. I'm actually tackling a similar problem except I need to return a JsonResult.

The (untested) NormalOrAjaxResult simply lets you specify an action result for the non ajax request and one for the ajax request. Because these are ActionResults you can mix up Redirect, View, Partial and Json view results.

public class NormalOrAjaxResult : ActionResult
{
    private readonly ActionResult _nonAjaxActionResult;
    private readonly ActionResult _ajaxActionResult;

    public NormalOrAjaxResult(ActionResult nonAjaxActionResult, ActionResult ajaxActionResult)
    {
        _nonAjaxActionResult = nonAjaxActionResult;
        _ajaxActionResult = ajaxActionResult;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var isAjaxRequest = context.HttpContext.Request["isAjax"];
        if (isAjaxRequest != null && isAjaxRequest.ToLower() == "true")
        {
            _ajaxActionResult.ExecuteResult(context);    
        } else
        {
            _nonAjaxActionResult.ExecuteResult(context);
        }
    }
}
有木有妳兜一样 2024-09-26 08:37:35

在这种情况下你不能只做两个不同的动作吗?您可以简单地将“共享”逻辑放入 [nonAction] 方法中吗?

can you not just make 2 different actions in that case? the 'shared' logic you could simply put in a [nonAction] method?

神也荒唐 2024-09-26 08:37:35

我知道我在这里的聚会真的迟到了,但这些对我来说似乎不太合适,所以这是我的 2 美分:

public class PartialViewConverter : ViewResult
{
    public ViewResultBase Res { get; set; }
    public PartialViewConverter(ViewResultBase res) { Res = res; }
    public override void ExecuteResult(ControllerContext context)
    {
        Res.ExecuteResult(context);
    }
    public static ViewResult Convert(ViewResultBase res)
    {
        return new PartialViewConverter(res);
    }
}

随着用法:

return PartialViewConverter.Convert(PartialView());

然后在你的控制器中,如果你重写 View

protected override ViewResult View(string viewName, string masterName, object model)
{
    //Whichever condition you like can go here
    if (Request.QueryString["partial"] != null)
        return PartialViewConverter.Convert(PartialView(viewName, model));
    else
        return base.View(viewName, masterName, model);
}

任何返回视图的操作方法也会自动当请求时返回部分内容:

public ActionResult Index()
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

    //This will return a partial if partial=true is passed in the querystring.
    return View();
}

I know I'm really late to the party here, but these didnt seem quite right to me, so here's my 2 cents:

public class PartialViewConverter : ViewResult
{
    public ViewResultBase Res { get; set; }
    public PartialViewConverter(ViewResultBase res) { Res = res; }
    public override void ExecuteResult(ControllerContext context)
    {
        Res.ExecuteResult(context);
    }
    public static ViewResult Convert(ViewResultBase res)
    {
        return new PartialViewConverter(res);
    }
}

With usage:

return PartialViewConverter.Convert(PartialView());

And then in your controller if you override View

protected override ViewResult View(string viewName, string masterName, object model)
{
    //Whichever condition you like can go here
    if (Request.QueryString["partial"] != null)
        return PartialViewConverter.Convert(PartialView(viewName, model));
    else
        return base.View(viewName, masterName, model);
}

Any action method where you return a view will automatically also return partials when requested:

public ActionResult Index()
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

    //This will return a partial if partial=true is passed in the querystring.
    return View();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文