12

We have been trying to get the Editor-Template to work with a dynamic property - to no avail. Maybe one of you can help us.

Here is roughly our class:

public class Criterion
{
    ...
    public string Text { get; set; }
    public dynamic Value { get; set; }
    public Type Type { get; set; }
    ...
}

Our razor view gets a model containg a list of sections which each contains a list of criteria in it. (We get these infos at runtime.) All these criteria should be displayed in edit mode - regarding their actual type: (excerpt)

@for (int i = 0; i < model.Sections.Count(); i++)
{
    for (int j = 0; j < model.Sections[i].Criteria.Count(); j++)
    {
        var criterion = model.Sections[i].Criteria[j];
        var type = criterion.Type.Name;
        var name = "Sections[" + i + "].Criteria[" + j + "].Value";
        var criterionDisplayName = criterion.Text;
        <label for="Sections_@(i)__Criteria_@(j)__Value">@criterionDisplayName</label>
        @Html.Editor(name, type)
    }
}

This does display for instance a checkbox correctly, but it does not use the value to set the checkbox status correctly (checked if the criterion.Value is true). Same goes for other types, like ints. (It does fill the form correctly after a POST request, but that is because MVC uses a temporary model to recreate the users input.)

As much as we have tried and researched: Is it even possible to use the Editor template with properties of type dynamic? If yes - how can we make it work? (We would not like to discern according to the possible type. We would like to have the MVC framework to use the right Editor template based on the actual type.)

toni
  • 2,270
  • 3
  • 22
  • 27

1 Answers1

17

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • It works with strongly typed view. I tried changing the Index-View to not use the strongly typed view: Deleted `@model IList` and used `@{ var model = (IList)Model; }`. Then I try `@Html.Editor("model[" + i + "].Value", "Criterion_" + model[i].Value.GetType().Name)`, but it gives me an error - _The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Int32'._ What am I doing wrong? Debugging tells me that GetType().Name is `Int32` in the Index-View. – toni Jul 16 '12 at 14:27
  • By the way - we need to use the not strongly typed view, because we are using a third party framework (ContentShape from Orchard). – toni Jul 16 '12 at 14:33
  • We ended up using Darins approach as above with `object`. We only had to cast the dynamic ViewModel and pass it to a strongly typed editor template. Thanks, Darin, for the valuable input. – toni Jul 19 '12 at 14:38
  • `EditorTemplates` are the way to go. – Oliver Feb 03 '14 at 21:45