Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:48:03+00:00 2026-06-07T15:48:03+00:00

We have been trying to get the Editor-Template to work with a dynamic property

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:48:05+00:00Added an answer on June 7, 2026 at 3:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get Twitter Bootstrap btn-group with dropdown to work for
I have been trying to get nServiceBus to work with Ninject 2.0 as the
I have been trying to get this one section of my UI to immediatly
I have been trying to get the Fusion Charts to work on Android 2.2
I have been trying to get this to work. Basically I have a search
I have been trying to get this to work for most of the day
Have been trying to get a fresh, just created rails application to work on
I have been trying to get a c++ program to use libcurl and can't
I have been trying to get jquery.autocomplete (http://docs.jquery.com/Plugins/autocomplete) to work for the last few
I have been trying to get my Arduino/Eclipse environment setup. For some reason I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.