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 8044321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:11:29+00:00 2026-06-05T05:11:29+00:00

Traditionally, I have built MVC applications using view models with Data Annotations attributes, and

  • 0

Traditionally, I have built MVC applications using view models with Data Annotations attributes, and I dynamically render the views using editor templates. Everything works great, and it really cuts down on the time it takes me to build new views. My requirements have recently changed. Now, I can’t define the view model at design time. The properties that will be rendered on the view are decided at run time based on business rules. Also, the validation rules for those properties may be decided at run time as well. (A field that is not required in my domain model, may be required in my view based on business rules). Also, the set of properties that will be rendered is not known until run time – User A may edit 6 properties from the model, while user B may edit 9 properties.

I am wondering if it is possible to create a model metadata provider that will supply my own metadata from business rules for an untyped view model like a collection of property names and values. Has anyone solved this problem?

  • 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-05T05:11:32+00:00Added an answer on June 5, 2026 at 5:11 am

    I solved a similar problem by creating a more complex model, and using a custom editor template to make the model be rendered to look like a typical editor, but using the dynamic field information:

    public class SingleRowFieldAnswerForm
    {
        /// <summary>
        /// The fields answers to display.
        /// This is a collection because we ask the MVC to bind parameters to it,
        /// and it could cause issues if the underlying objects were being recreated
        /// each time it got iterated over.
        /// </summary>
        public ICollection<IFieldAnswerModel> FieldAnswers { get; set; } 
    }
    
    public interface IFieldAnswerModel
    {
        int FieldId { get; set; }
    
        string FieldTitle { get; set; }
    
        bool DisplayAsInput { get; }
    
        bool IsRequired { get; }
    
        bool HideSurroundingHtml { get; }
    }
    
    // sample implementation of IFieldAnswerModel
    public class TextAreaFieldAnswer : FieldAnswerModelBase<TextAreaDisplayerOptions>
    {
        public string Answer { get; set; }
    }
    

    EditorTemplates/SingleRowFieldAnswerForm.cshtml:

    @helper DisplayerOrEditor(IFieldAnswerModel answer)
    {
        var templateName = "FieldAnswers/" + answer.GetType().Name;
        var htmlFieldName = string.Format("Answers[{0}]", answer.FieldId);
    
        if (answer.DisplayAsInput)
        {
            @Html.EditorFor(m => answer, templateName, htmlFieldName)
            // This will display validation messages that apply to the entire answer. 
            // This typically means that the input got past client-side validation and
            // was caught on the server instead.
            // Each answer's view must also produce a validation message for
            // its individual properties if you want client-side validation to be
            // enabled.
            @Html.ValidationMessage(htmlFieldName)
        }
        else
        {
            @Html.DisplayFor(m => answer, templateName, htmlFieldName)
        }
    }
    <div class="form-section">
        <table class="form-table">
            <tbody>
                @{
                    foreach (var answer in Model.FieldAnswers)
                    {
                        if (answer.HideSurroundingHtml)
                        {
                            @DisplayerOrEditor(answer)
                        }
                        else
                        {
                            var labelClass = answer.IsRequired ? "form-label required" : "form-label";
                            <tr>
                                <td class="@labelClass">
                                    @answer.FieldTitle:
                                </td>
                                <td class="form-field">
                                    <div>
                                        @DisplayerOrEditor(answer)
                                    </div>
                                </td>
                            </tr>
                        }
                    }
                }
            </tbody>
        </table>
    </div>
    

    So I populate my SingleRowFieldAnswerForm with a series of answer models. Each answer model type has its own editor template, allowing me to customize how different types of dynamic “properties” should be displayed. For example:

    // EditorTemplates/FieldAnswers/TextAreaFieldAnswer.cshtml
    
    @model TextAreaFieldAnswer
    
    @{
        var htmlAttributes = Html.GetUnobtrusiveValidationAttributes("Answer", ViewData.ModelMetadata);
        // add custom classes that you want to apply to your inputs.
        htmlAttributes.Add("class", "multi-line input-field");
    }
    @Html.TextAreaFor(m => m.Answer, Model.Options.Rows, 0, htmlAttributes)
    @Html.ValidationMessage("Answer")
    

    The next tricky part is that when you send this information to the server, it doesn’t inherently know which type of IFieldAnswerModel to construct, so you can’t just bind the SingleRowAnswerForm in your arguments list. Instead, you have to do something like this:

    public ActionResult SaveForm(int formId)
    {
        SingleRowAnswerForm form = GetForm(formId);
        foreach (var fieldAnswerModel in form.FieldAnswers.Where(a => a.DisplayAsInput))
        {
            // Updating this as a dynamic makes sure all the properties are bound regardless
            // of the runtime type (since UpdateModel relies on the generic type normally).
            this.TryUpdateModel((dynamic) fieldAnswerModel, 
                string.Format("Answers[{1}]", fieldAnswerModel.FieldId));
            }
        ...
    

    Since you provided MVC with each dynamic “property” value to bind to, it can bind each of the properties on each answer type without any difficulty.

    Obviously I’ve omitted a lot of details, like how to produce the answer models in the first place, but hopefully this puts you on the right track.

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

Sidebar

Related Questions

Traditionally when using a DbCommand when retrieving data from a sproc, something like the
I have traditionally implemented a Model-View-Presenter [Passive View] like so: interface IView { string
I'm currently using the mysqli php extension. Traditionally I have used mysqli_real_escape_string to escape
I have an app that's built for Android 2.2, so I'm not using the
I have experience developing web applications using Java, HTML, JS, and CSS. At this
I have a pretty common layout issue that I have traditionally used a table
I have written a webapp using traditional cgi. I'm now trying to rewrite it
I'm using a traditional Zend framework application generated using Zend tool . I have
Most web frameworks are still using the traditional action based MVC model. A controller
I'm looking to build an ASP.NET MVC application where many screens will have multiple

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.