2
public ActionResult Index() { return View("ViewName"); // cshtml file name } 

That is normally works.

public ActionResult Index() { string razor = "<p>Date: @DateTime.Now</p>"; return View(razor); } 

Can I do something like that? Not render a .cshtml file, render a string...

-- Edit------

I actually create .cshtml files programmatically. For example i gonna use @Html.TextBoxFor(...) or foreach statement in my string. – E-D just now edit

3 Answers 3

1

Does this answer your question:

Action:

public ActionResult Index() { HtmlString razor = new HtmlString(string.Format("<p>Date: {0}</p>", DateTime.Now.ToString())); return View(razor); } 

View Index.cshtml:

@model HtmlString @Model 

Solution 2, implement this in your Controller, a view is not needed:

public HtmlString Index() { return new HtmlString("<p>Hello World!</p>"); } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use ContentResult:

return Content(String.Format("<p>Date: {0}</p>",DateTime.Now)); 

1 Comment

i actually create .cshtml files programmatically. For example i gonna use @Html.TextBoxFor(...) or foreach statement in my string.
0

RazorEngine solves my problem exactly. Thanks everybody. http://razorengine.codeplex.com/

1 Comment

How did you use it in your case? I'm having similar issues.. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.