How To Create An Htmlhelper Extension Method That Will Bind An Ienumerable To A Table
This is my view model: public class TaskViewModel{ public int TaskID{get;set;} public IEnumerable Executors{get;set;} } public class TaskExecutor{ public
Solution 1:
One option could be as follows:
namespaceSystem.Web.Mvc
{
publicstaticclassExecutorsExtensions
{
publicstatic MvcHtmlString Executors(this HtmlHelper helper, List<TaskExecutor> executors)
{
var sb = new StringBuilder();
sb.Append("<table>");
for (var i = 0; i < executors.Count; i++)
{
sb.Append("<tr>");
sb.Append(string.Format("<td><input name=\"Executors[{0}].FirstName\" value=\"{1}\"></td>", i, executors[i].FirstName));
// add other cells here
sb.Append("<tr>");
}
sb.Append("</table>");
returnnew MvcHtmlString(sb.ToString());
}
}
}
Usage
@Html.Executors(Model.Executors)
Please note you would need to make the Executors a List<TaskExecutor>
for the indexing to work properly.
The indexing of the loop and and name variable would keep the model binding happy. You could add further fields where I have commented above.
You could also use Html.TextBox
or Html.TextBoxFor
to generate the inputs if needed.
Post a Comment for "How To Create An Htmlhelper Extension Method That Will Bind An Ienumerable To A Table"