HtmlHelper.EditorFor from Controller
my problem is the following I have an instance of a class and a HtmlTextWriter. Now i want to generate the Editor like in the View. The HtmlHelper is present and the methods Editor, TextBox ... too.
public class Class
{
public string Test { get; set; }
}
HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
Class a = new Class(); //
a.Test = "Lorem ipsum";
how can i do this to get a TextBox and all the goodness of Validations (DataAnnotations) 开发者_StackOverflow?
writer.Write(HtmlHelper.Editor(x => ....).ToString());
Have a nice day !
To get the DataAnnotations validation (using unobtrusive JS) you need a FormContext, which is created when you use the BeginForm helper.
This is Probably not the direct answer to your question. However, you can take a slightly different approach and put everything you want in a view and then render that view as a string. Look at this question. it describes how you can render view to a string.
精彩评论