0

I know how to create views in MVC.

But I am searching for the idea to dynamically add a create type view for which I will pass the scafolding Model as parameter to the controller method. I cannot go manually but I want to add view during some controller action.

I want to have a code something look like...

public class SomeController : Controller
{
    public void SomeAction(string modelName)
    {
        //add view of create type 
    }
}
6
  • Are you able to clarify your question a little? I'm not sure if it's entirely clear. Are you hoping to create new Views (files) with this? I.e. the user submits code which is then used as the code for a new view? Is security a concern here? Commented Aug 12, 2014 at 4:35
  • @RowanFreeman All I want to do is to add a view programmatically by passing a 'modelName' as argument to a controller action. Commented Aug 12, 2014 at 5:25
  • Can you just do return View(modelName)? Commented Aug 12, 2014 at 5:49
  • No, Rowan Freeman . I do not have a view yet but just want to create now programmatically. Commented Aug 12, 2014 at 6:01
  • So, as I asked in my first comment, are you wanting to create a new file? modelName.cshtml? Commented Aug 12, 2014 at 6:06

1 Answer 1

3

You may want to check out RazorEngine that will give you greater control to build robust templates. Based on "Create Type", you can select specific view and pass model object to the view. The result of Parse method will be plain html.

  string template = "Hello @Model.Name! Welcome to Razor!";
  string result = Razor.Parse(template, new { Name = "World" });

If you want to achieve something for which RazorEngine is of no help, please clarify your requirement a bit more.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried doing this using Razor nuGet package but I end up with the problem of 'Could not load file or assembly 'RazorEngine, Version=3.4.1.0...'. I checked .NET 4.5.1 is installed in my machine.
Did you installed it using "Install-Package RazorEngine"? Were it installed successfully? I did just now, it was giving an error then I removed "dependentAssembly" section for "System.Web.Razor" from web.config. It all worked then.
After successful installation of RazorEngine, I cannot find such a code in web.config. i.e. I cannot get "dependentAssembly" section for "System.Web.Razor". I am using Visual Studio 2013 ultimate edition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.