ASP.NET MVC - HybridViewResult (ViewResult /PartialViewResult)
是否可以构建一个混合 ViewResult,根据 AjaxRequest 或 HttpRequest 返回 PartialViewResult
或 ViewResult
?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
添加任何构造函数 &根据需要获取InnerViewResult变量,即传递模型。
Try:
Add any constructor & GetInnerViewResult variations as needed i.e. to pass Model.
这是对 eglasius 答案的稍微精简的看法。我实际上正在解决类似的问题,只是我需要返回 JsonResult。
(未经测试的)NormalOrAjaxResult 只是让您为非 ajax 请求指定一个操作结果,为 ajax 请求指定一个操作结果。因为这些是 ActionResults,所以您可以混合重定向、视图、部分和 Json 视图结果。
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.
在这种情况下你不能只做两个不同的动作吗?您可以简单地将“共享”逻辑放入
[nonAction]
方法中吗?can you not just make 2 different actions in that case? the 'shared' logic you could simply put in a
[nonAction]
method?我知道我在这里的聚会真的迟到了,但这些对我来说似乎不太合适,所以这是我的 2 美分:
随着用法:
然后在你的控制器中,如果你重写 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:
With usage:
And then in your controller if you override View
Any action method where you return a view will automatically also return partials when requested: