I am re-opening my question as it has been set as duplicate while it is not – or people who tagged it as duplicate should explain me why it is a duplicate……….
https://stackoverflow.com/questions/13227988/html-displayfor-result
How can I get the result of the method Html.DisplayFor() in a C# Class, like in a View model or even in a Controller ? And not in the View Aspx or Razor.
[Edit]
In fact I have a table to display and depending on the number of record I use a Telerik table or a simple HTLM table.
For the moment I have a function in my view to get the string to display for each column so I use the same format for both tables.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ViewModels.OrderViewModel>" %>
<script runat="server" type="text/C#">
public string GetCellValue (String columnName, Order order)
{
string value = "";
switch (columnName)
{
case "Ref":
value = order.order.Reference.ToString(); break;
case "Etc":
value = Html.DisplayFor(p => order.order.Etc).ToHtmlString();break;
case "Payment date":
foreach (Executions ex in order.executions)
{
value += ex.Date.Date.ToString("yyyy-MM-dd") + " ";
}
break;
I want to move this function out of my view in the ViewModel for example, but I do not know how I can call the function Html.DisplayFor() out of a view.
Don’t understand why would you want to do it, but…
After your edit, if you move GetCellValue to another place, maybe you will only need to do
as, it should only display its value as string (unless you have set up a template for it).