0

I can't find a way to build a ViewResult from a simple html string. I tried many solutions from simple to more elaborated, but it seems that I always lose the (master page / layout) in the process.

Here is a solution that works, obviously since I don't do anything special here :

public class MyActionResult : ViewResult
{
    public MyActionResult(controller controller, object model)
    {
        this.ViewName = "MyView";
        this.ViewData.Model = model;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        base.ExecuteResult(context);
    }

}

From this working result that is equal to just calling View("MyView", model) from the action, I went to :

public class MyActionResult : ViewResult
{
    public MyActionResult(GenericController controller, object model)
    {
        this.View = new MyView(controller, model);
    }

    public override void ExecuteResult(ControllerContext context)
    {

        base.ExecuteResult(context);
    }

}

public class MyView : IView
{
    public MyView(GenericController controller, object model)
    {
    }

    public void Render(ViewContext viewContext, TextWriter writer)
    {
        writer.Write("TEST");
    }
}

Still working, but no more (master page / layout), just the plain TEST. So what's wrong doctor ?

EDIT : I feel like I need to be more specific on my objective. I have about ten cshtml pages that I have to copy among all my MVC projects because they are used by all of them. These pages are filled with just one method call using HtmlHelper extension like :

@Html.MvcSharedMenu()

In these extension methods I work with MvcHtmlStrings to build my html, I guess I could call these my "Custom Controls". I know about Razor Generator, but I'd like to avoid that solution especially when all the code is already outside of the View.

Glopper
  • 23
  • 4
  • "master pages" only apply to ASP.NET WebForms, and to an extent, the (legacy) ASPX view-engine for ASP.NET MVC - they do not otherwise exist in ASP.NET MVC. Razor has a similar (but more flexible) system called "Layouts" which work slightly different to ASPX's Master pages. Please clarify your question. – Dai Apr 24 '17 at 17:15
  • "master page" is the wrong term, but if he had it showing and then it disappeared it's safe to say that he's using a layout. – Scott Hannen Apr 24 '17 at 17:20
  • Actually, I disagree, Microsoft refers to it as a master page as well when returning a View() in the MVC MSDN documentation, whether either name is semantics can be a different discussion, we all know Microsoft's meticulous use of naming conventions in it's documentation if you have read the Microsoft Manual of Style, so in that they call it a master page at all can give the go ahead to allow you to call it that also if you wish: https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view(v=vs.118).aspx#M:System.Web.Mvc.Controller.View%28System.String,System.String,System.Object%29 – Nard Dog Apr 24 '17 at 17:22
  • I saw the answer that you deleted, Controller.View Method (String, String) That looked like a workable answer. The only thing I didn't like is that we don't usually specify the layout in the controller - we let the view or viewstart specify what the layout is. – Scott Hannen Apr 24 '17 at 17:27
  • Indeed, I use a layout, sorry for breaking the naming convention. I'll edit the message. – Glopper Apr 24 '17 at 17:31

1 Answers1

0

After reading the edits, perhaps what you want is this:

return View("~/pathToYourView/View.cshtml", yourViewModel);

That doesn't keep you from having to create a .cshtml file, but it keeps you from having to create ten of them. If the model for each is just a string then all of your controller methods can share the same view.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
  • I still have to copy the cshtml among all my projects or to use some kind of shared project / folder to point to. If there's no other way, then I'll try to go with Razor Generator. – Glopper Apr 24 '17 at 18:27