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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:34:30+00:00 2026-05-25T14:34:30+00:00

I am implementing a MVC3/Razor web application that retrieves some of its fields a

  • 0

I am implementing a MVC3/Razor web application that retrieves some of its “fields” a user can edit from other services so the list of properties to edit in a view is completely dynamic and unknown at compile time.

I wrote some partial-view and HTML-helpers that loop through the groups and properties retrieved from the other services. Now I am at the point where I have to build the tags for the various property types and thought why not re-use the MVC editor template system for this? There is support for various data-types (eg. checkboxes etc.) and one even customize them with my custom templates.

So far so good but how can I use Html.EditorFor() or Html.Editor() for custom data objects/properties? Meaning for building dynamic forms without static typed data as the view model.

Here is a minimalistic sample of my HTML helper code:

public static MvcHtmlString GetField(this HtmlHelper helper, Field field)
{
...
   return helper.EditorFor(field, m => m.Value);
...
}

The property “field” is the field I got from the external services. It has a “Value” property of type object. I like to build the editor code for this property-type.

As I understand the editor templates are built on the current view model. Can I pass another object as the model then the current view model (eg. in my above example “field”)?

Any help would be great!

Cheers,
Marc

  • 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-05-25T14:34:30+00:00Added an answer on May 25, 2026 at 2:34 pm

    My first ASP MVC task involved building a dynamic form, and it isn’t straight forward.
    Basically you can’t use the built in helpers or validation because they expect strongly typed objects.

    My view basically loops through the input fields, checks the DataType (bool, int, string, datetime etc) and builds an editor directly for that type.

    You also have to do all your validation manually, decorating the types with attributes to do this doesn’t work, see my question if(ModelState.IsValid) doesn’t work with FormsCollection. What to use instead?

    Razor View logic (we use DevExpress MVC extensions, but you get the drift)
    The form object and it’s Fields collection are our bespoke objects that describe how the form should look (the page gathers criteria for searches, which is why you’ll see criteria and search type names in the code).

    <table id="criteriaTable">
    @foreach (var field in form.Fields)
    {
        <tr id="criteriaTableRow">
        @if (field.IsVisible)
        { 
        <td id="criteriaTableLabelCol"> 
           @Html.DevExpress().Label(s => s.Text = field.Caption).GetHtml()
        </td>
        <td id="criteriaTableEditCol">
            @if (field.Type == typeof(bool))
            {
               @Html.CheckBox(s =>
          {
              s.Checked = field.IsBoolSet;
              s.Name = field.Name;
              s.ClientEnabled = !field.IsReadonly;
          }).GetHtml()
            }
            else if (field.Type == typeof(DateTime))
            {
                Html.DevExpress().DateEdit(s =>
                {
                    s.Name = field.Name;
                    s.ClientEnabled = !field.IsReadonly;
                    if (!string.IsNullOrEmpty(field.Value))
                    {
                        DateTime dateValue;
                        if (DateTime.TryParse(field.Value, out dateValue))
                            s.Date = dateValue;
                    }
                }).GetHtml();
            }
            else if (field.ListValues.Count > 0)
            {
                <input type="hidden" id="@("initiallySelected" + field.Name)" value="@field.Value" />
                Html.DevExpress().ListBox(s =>
                {
                    s.Name = field.Name;
                    s.ClientVisible = field.IsVisible;
                    s.ClientEnabled = !field.IsReadonly;
                    s.Properties.SelectionMode = DevExpress.Web.ASPxEditors.ListEditSelectionMode.CheckColumn;
                    s.Properties.TextField = "Name";
                    s.Properties.ValueField = "Value";
                    s.Properties.ValueType = typeof(string);
    
                    //s.Properties.EnableClientSideAPI = true;
                    foreach (var item in field.ListValues)
                    {
                        s.Properties.Items.Add(item.Name, item.Value);
                    }
    
                    //s.Properties.ClientSideEvents.SelectedIndexChanged = "MultiSelectListChanged";
                    s.Properties.ClientSideEvents.Init = "MultiSelectListInit";
                }).GetHtml();
    
            }
            else
            {
                //Html.TextBox(field.Name, field.Value)
                Html.DevExpress().TextBox(s =>
                {
                    s.Name = field.Name; s.Text = field.Value;
                }).GetHtml();
            }
            @Html.ValidationMessage(field.Name)
            <input type="hidden" name="@("oldvalue_" + field.Name)" value="@field.Value" />
            <input type="hidden" name="@("olduse_" + field.Name)" value="@(field.IncludeInSearch ? "C" : "U")" />
        </td>
        <td id="criteriaTableIncludeCol"> 
            @Html.DevExpress().CheckBox(s =>
       {
           s.Checked = field.IncludeInSearch;
           s.Name = "use_" + field.Name;
           s.ClientEnabled = (!field.IsMandatory);
       }).GetHtml()
        </td>
        }
        </tr>
    }
        </table>
    

    The Controller action accepts a Forms Collection. I loop through the formsCollection looking for the control names I specified in the view.

    [HttpPost]
    public ActionResult QueryCriteria(FormCollection formCollection)
    {
        var isValid = true;
        foreach (var field in form.Fields)
        {
            var value = (formCollection[field.Name] ?? "").Trim();
            ...
    

    If there are any validation errors I can specify control level validation by adding a ModelError directly to the model e.g.

    ModelState.AddModelError(field.Name, "This is a mandatory field");
    

    and I return the View if there are validation errors.

    Hope this helps.

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

Sidebar

Related Questions

I have an MVC3 application that I am implementing pjax into . Everything is
I am implementing a web application using ASP.NET MVC3. In the application I want
Implementing a Thread by providing a new class that extends Thread and overriding its
Implementing a web service that uses Transport-level security with WCF over HTTP is pretty
I am implementing a new ASP.NET MVC 3 application that will use a form
Have an ASP.NET MVC3 app with model validation using FluentValidation . User enters some
I'm currently implementing a website that require a customer support user to Login as
When implementing the ViewModel in a Model-View-ViewModel architecture WPF application there seem to be
My MVC3 application is using an SQL Server 2008 to store data. In particular
I have an MVC3 + EF 4.1 application, against a SQL Server 2008 database,

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.