2012-02-24 126 views
2
public ActionResult Index() 
{ 
    return View("ViewName"); // cshtml file name 
} 

这是正常的作品。渲染字符串作为剃刀cshtml

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

我可以这样做吗?不渲染.cshtml文件,呈现字符串,...

- 编辑------

其实我编程方式创建.cshtml文件。例如,我会在我的字符串中使用@ Html.TextBoxFor(...)或foreach语句。 - ED刚才编辑

回答

1

这是否回答你的问题:

操作:

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

查看Index.cshtml:

@model HtmlString 

@Model 

方案二,实现这个在您的控制器,视图不需要:

public HtmlString Index() 
{ 
    return new HtmlString("<p>Hello World!</p>"); 
} 
0

尝试使用ContentResult

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

我实际上以编程方式创建.cshtml文件。例如,我会在我的字符串中使用@ Html.TextBoxFor(...)或foreach语句。 – 2012-02-24 13:57:42

0

RazorEngine正好解决了我的问题。谢谢大家。 http://razorengine.codeplex.com/

+0

你是怎么使用它的?我有类似的问题..谢谢! – jpgrassi 2014-11-21 14:07:06

+0

什么样的问题? – 2014-11-24 09:08:24