2

I have a partial view file which receives a @model saved outside of the views directory - actually stored in app_data.

However I get error:

The name 'model' does not exist in the current context
Source File: ...\app_data\_DefaultLayout.cshtml

Below is my code:

 @Html.Partial("~/app_data/_DefaultLayout.cshtml"), Model)

Default Layout is as follows:

 @using System.Web.Mvc
 @using System.Web.Mvc.Html
 @model MyApp.ViewModels.CreateCaseViewModel

 // some html and razor code

2 Answers 2

3

This exception is a compilation error on the cshtml file. The view is not having access to model type. If you check your Solution Explorer, there are two web.config files, one in the Views folder and one at root level.

One way to sort it is making a copy of Views/web.config into App_Data.

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

Comments

2

App_Data is a special directory in ASP.NET, which is supposed to store data files like XML files / mdf files to store data. It has restricted access as well.

You should not be putting any UI layer concerns( views) in that folder. Asp.NET MVC has a convention for your UI layers, which is ~/Views directory. So use that location to keep your views.

If absolutely needed, you can put your view files pretty much anywhere in the application (Except those system directories), For example, you can create a directory called MyPartialViews and put your partial view there and refer the full path to that. The important thing to remember is, you need to have a web config in that folder (copy the one from the ~/Views and put it there). The web config has many needed elements in that including pageBaseType for all views

4 Comments

yes I'm aware of that convention. However I need to allow users to upload layout files and I'm unable to grant them access to do that inside the views folder for obvious security reason.
You should not be allowing users to upload layout files. What if they upload some files, which has some C# code for recursive loop in that ? What if it has code to read files from your app root and display it in the ui ? The code gets executed in server and people can mis use it. That is a security concern. If all you care about is the html markup/styles, you should consider something like an editor ui where they can edit the page (like a rich text editor) so that you store that markup in a table and render it as needed (like a blog)
I need them to be able to layout a form where the structure of the form is stored as xml or json. Using razor was by far easier than xslt which is why I'm storing the layout using razor. The other option would be to construct the form using xslt and allow users to upload and xslt file to layout their form.
would xslt be safer?

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.