私のシナリオを説明することは、私が達成しようとしていることを説明するための最良の方法になります。それが存在する場合、私はよりクリーンな解決策を探しています。
ロックしたいコンテンツがあります。さまざまなタイプを使用できるため、ロック解除モデルを抽象化しました。リダイレクトまたはレンダリングされた部分ビュー、または将来表示される可能性のある他の何かである可能性があるため、ActionResultを返すことにしました。
public abstract class AContentUnlocker
{
public abstract ActionResult GetUnlockActionResult();
}
public class RedirectUnlocker : AContentUnlocker
{
public override ActionResult GetUnlockActionResult()
{
return new RedirectResult("http://www.url1.com?returnUrl=mywebsiteagain");
}
}
public class PartialViewUnlocker: AContentUnlocker
{
public override ActionResult GetUnlockActionResult()
{
PartialViewResult view = new PartialViewResult();
view.ViewName = "_PartialViewToUnlock";
return view;
}
}
私のコンテンツは、適切なロック解除メカニズムを備えたモデルで表されます
public class MyContent
{
public string Description { get; set; }
public AContentUnlocker ContentUnlocker { get; set; }
}
私のコントローラーでは、適切なロック解除メカニズムが設定された目的のコンテンツを返すだけです。
public ActionResult Index()
{
MyContent myContent = new MyContent() {
Description = "Content 1",
ContentUnlocker = new PartialViewUnlocker()
};
return View(myContent);
}
次に、インデックスビューで、ActionResultを実行します。
@{
Model.ContentUnlocker.GetUnlockActionResult().ExecuteResult(this.ViewContext);
}
return View(myContent);
RedirectActionResultは正常に機能します。私の問題は、部分ビューアクションの結果では、MVCの実行サイクルを考えると、部分ビューがコントローラービューの前にレンダリングされることです。だから私は次のようなものを手に入れています:
<!-- html of the partial view rendered -->
<div> blah blah </div>
<!-- html of the parent view -->
<html>
<head></head>
<body> blah .... </body>
</html>
それが可能かどうかはわかりませんが、Html.RenderPartialと同じようにActionResultを実行する方法はありますか?