Dynamics don't fit the bill nicely with ASP.NET MVC. They remind me about ViewBag
and I hate ViewBag
from the very deep fabrics of my body. So I would take a different approach.
Let's take for example the following model:
public class Criterion
{
public string Text { get; set; }
public object Value { get; set; }
}
Value could be any type that you wish to handle.
Now you could have a controller which populates this model:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new[]
{
new Criterion { Text = "some integer", Value = 2 },
new Criterion { Text = "some boolean", Value = true },
new Criterion { Text = "some string", Value = "foo" },
};
return View(model);
}
}
and then a corresponding view:
@model IList<Criterion>
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
<div>
@Html.LabelFor(x => x[i], Model[i].Text)
@Html.EditorFor(x => x[i].Value, "Criterion_" + Model[i].Value.GetType().Name)
</div>
}
<button type="submit">OK</button>
}
Now for each type that you want to handle you could define a corresponding editor template:
~/Views/Shared/EditorTemplates/Criterion_String.cshtml
:
@model string
@Html.TextBoxFor(x => x)
~/Views/Shared/EditorTemplates/Criterion_Boolean.cshtml
:
@model bool
@Html.CheckBoxFor(x => x)
~/Views/Shared/EditorTemplates/Criterion_Int32.cshtml
:
@model int
@{
var items = Enumerable
.Range(1, 5)
.Select(x => new SelectListItem
{
Value = x.ToString(),
Text = "item " + x
});
}
@Html.DropDownListFor(x => x, new SelectList(items, "Value", "Text", Model))
Obviously displaying this model in the view is only the first step. I suppose that you will want to get the values that the user entered back in the POST controller action for some processing. In this case some small adaptations are necessary. We need to add a custom model binder that will be able to instantiate the correct type at runtime and include the concrete type as hidden field for each row. I have already shown an example in this post
. Also notice in this example that I used a base class instead of directly working with the object
type.