Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...