Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC 3 Editor for dynamic property

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.)

like image 606
toni Avatar asked Jul 14 '12 18:07

toni


1 Answers

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.

like image 196
Darin Dimitrov Avatar answered Nov 04 '22 04:11

Darin Dimitrov