I want to build a dynamic form from the scratch, and i already created the following elements:
A xml file with all fields that i want to show:
<?xml version="1.0" encoding="utf-8" ?>
<Fields>
<FieldList>
<Field>
<Type>checkbox</Type>
<Name>gender</Name>
<DefaultValue>false</DefaultValue>
<CssClass>myclass</CssClass>
</Field>
<Field>
<Type>textbox</Type>
<Name>name</Name>
<DefaultValue>fill name</DefaultValue>
<CssClass>formtextbox</CssClass>
</Field>
</FieldList>
</Fields>
A view typed to a class that i used to deserialize the xml:
@model Fields
@{
if(Model!=null)
{
using (Html.BeginForm())
{
foreach (Field field in Model.FieldList)
{
switch(field.Type)
{
case "textbox":
@Html.TextBox(field.Name, field.DefaultValue);
break;
case "checkbox":
@Html.CheckBox(field.Name,Convert.ToBoolean(field.DefaultValue));
break;
}
}
}
}
}
By the moment the app is working properly but i have the following doubts:
1) I think is not an elegant solution since i have to put too many code in the view right?
2) I would like to add now some server validation. I normally work with data annotations, but here is not possible i guess since i dont know how is the thing that im submitting. Is there any way to create on the fly an class instance and add data anotations on the fly?
3) I created another action and controller that will handle the post. Since i dont know what im submitting my action doesnt receive as parameter nothing. I guess i have to use Request.Form from my action right?