问题描述
是否有可能建立一个混合的ViewResult,在一个AjaxRequest的depedency返回或Htt的prequest一个 PartialViewResult
或的ViewResult
?
Is it possible to build a hybrid ViewResult that returns in depedency of an AjaxRequest or HttpRequest a PartialViewResult
or ViewResult
?
IsAjaxRequest - >回PartialViewResult
!IsAjaxRequest - >返回的ViewResult
IsAjaxRequest --> return PartialViewResult!IsAjaxRequest --> return ViewResult
据我知道我应该HybridViewResult从ViewResultBase派生。
As far as I know my HybridViewResult should derive from ViewResultBase.
但如何实现FindView方法?
But how to implement the FindView method?
推荐答案
尝试:
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
}
}
添加任何构造函数和放大器; GetInnerViewResult变化需要,即通过模型。
Add any constructor & GetInnerViewResult variations as needed i.e. to pass Model.
这篇关于ASP.NET MVC - HybridViewResult(的ViewResult / PartialViewResult)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!