Udało mi się to zrobić w ten sposób:
public ActionResult Index()
{
XElement xml = XElement.Load(Server.MapPath("~/App_Data/form.xml")); //.Load("~/Content/form.xml");
ViewData["xml"] = xml;
return this.View();
}
xml:
<?xml version="1.0" encoding="utf-8" ?>
<form name="form1">
<section number="1">
<field type="text" name="First name" class="textbox" />
</section>
<section number="2">
<field type="text" name="Biography" class="textarea" />
</section>
</form>
następnie dodaje do folderu Shared kolejny folder i nazywam go "EditorTemplates" w którym mam:
checkbox.cshtml:
@Html.CheckBox("name")
oraz textbox.cshtml:
@Html.TextBox("name")
następnie w widoku Index mam:
@using System.Xml.Linq
@using Microsoft.Ajax.Utilities
@{
XElement xml = (XElement)ViewData["xml"];
}
@using (Html.BeginForm())
{
var q = from s in xml.Descendants("section") select s;
foreach (var sec in q)
{
<fieldset>
<legend>Section @sec.Attribute("number").Value</legend>
<table>
@{
var qq = from f in sec.Descendants("field") select f;
foreach (XElement field in qq)
{
<tr>
<td> @Html.Editor("", (string)field.Attribute("class"))</td>
</tr>
}
}
</table>
</fieldset>
}
<input type="submit" value="submit"/>
}
co myślisz?