Best way to render an object to html
What is the best way to render a data object into html?
Take into your consideration that my object should be dynamically drawn on the page and it will be consisted of multiple controls (Labels, TextBoxes, CheckBoxes, DropdownLists ... etc).
My object is a report 开发者_JS百科which contains sections.
Each sections contains groups.Groups should be drawn as an Accordion view.
Groups contains fields which will be rendered as Labels,TextBoxes, CheckBoxes or maybe DropdownLists according to the specified field type.
What is the best way to render that object?
I'm using asp.net mvc 2
Look at DisplayTemplates.
It works like this: You have an ascx file "partial view", vith the same name as your class and "strongly typed". Put it in /Views/Shared/DisplayTemplates".
Then you can just use Html.Display("varName","className"); to show your object with the DisplayTemplate.
Controller:
ViewData["var"] = new MyClass();
View:
Html.Display("var","MyClass");
Views/Shared/DisplayTemplates/MyClass.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyClass]>" %>
<div class="Hey">
<!-- code for showing my class here -->
<% Model.Property; //Model is of type MyClass
%>
</div>
http://www.asp.net/mvc/videos/mvc2-template-customization
http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx
The best way is to create a View, then use the Html
helper class/methods. See here, for more info.
Something like this should get you started:
<% Html.TextBox("stringData", Model.YourString) %>
<% Html.CheckBox("boolData", Model.YourBool) %>
精彩评论