Let’s say i have 2 files located in the same folder.
/Test/View.cshtml
<h1>File that needs to be loaded in to a string</h1>
/Test/Content.cs
public class Content {
public string GetView()
{
Return View("/Test/View.cshtml",someModel)
}
}
It should not cair about the RouteData from Web.Config
The point of doing this is, so that i am able to retrieve the GetView and use it elsewhere.
I know this question and approch is wierd but i am in a uniq situation developeing a CMS system, so i really need something like this.
How could i achieve this :)?
Update: Explanation
_Layout.cshtml
This file has no RenderBody as it normally has. Instead it has different Areas like this one.
@{
Render r = new Render("Content");
}
@r.Print()
Each area are printing out different modules, fx: a newsletter or a gallery. And for that to be possible this is done:
public interface IModule
{
string Name { get; set; }
int Id { get; set; }
string View();
}
public class ModuleList
{
public List<IModule> Modules = new List<IModule>();
public ModuleList()
{
Modules.Add(new ContentView() { Name = "Content" });
Modules.Add(new GalleryView() { Name = "Gallery" });
Modules.Add(new NewsletterView() { Name = "Newsletter" });
}
}
And here is is the ContentView Class (One of many Modules)
public class ContentView: IModule
{
public string Name { get; set; }
public int Id { get; set; }
DbModulesDataContext db = new DbModulesDataContext();
public string View()
{
var q = (from c in db.mContents
where c.Id == Id
select c).FirstOrDefault();
return ("<h1>"+q.Html+"</h1>");
}
}
As you can see right now the html is inline with the c# but i want it the other way around. (i want the View() to work together with a cshtml file)
Does it make a little more sence now?
I finaly found my solution!
It’s right there!
http://razorengine.codeplex.com/
Thank you for trying though 🙂