I am creating configurable forms in MVC which will contain dynamic controls base off of this SO post. The controls are all built from my base ControlViewModel
which just contains properties for all of the controls
public abstract class ControlViewModel
{
public abstract string Type { get; }
public bool Visible { get; set; }
public string Label { get; set; }
public string Name { get; set; }
}
Each control type is defined seperately by inheriting ControlViewModel
public class TextBoxViewModel : ControlViewModel
{
public override string Type
{
get { return "textbox"; }
}
public string Value { get; set; }
}
I have text boxes, check boxes, and drop downs all defined in a similar manner. The issue I am having is when the controls are displayed on the page, their name
and id
attributes are not rendering as expected. In my controller I have
public ActionResult Index()
{
var model = new MyViewModel
{
Controls = new ControlViewModel[]
{
new TextBoxViewModel
{
Visible = true,
Label = "label 1",
Name = "TextBox1",
Value = "value of textbox"
}
}
}
return View(model)
}
In my Index.cshtml
I render each control like so:
@model DynamicForms.Models.MyViewModel
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Controls.Length; i++)
{
if (Model.Controls[i].Visible)
{
<div>
@Html.HiddenFor(x => x.Controls[i].Type)
@Html.HiddenFor(x => x.Controls[i].Name)
@Html.EditorFor(x => x.Controls[i])
</div>
}
}
<input type="submit" value="OK" />
}
The editor just renders the control and the label
@model DynamicForms.Models.TextBoxViewModel
@Html.LabelFor(x => x.Value, Model.Label)
@Html.TextBoxFor(x => x.Value)
The issue is that when the page renders, the name
and id
attributes don't render as the actual string
values, but instead as the type
<div>
<input id="Controls_0__Type" name="Controls[0].Type" type="hidden" value="textbox">
<input id="Controls_0__Name" name="Controls[0].Name" type="hidden" value="TextBox1">
<label for="Controls_0__Value">label 1</label>
Does anyone know how I can populate the name
and id
attributes correctly here?