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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:55:09+00:00 2026-06-14T06:55:09+00:00

I’m having issues getting my controller to recognize the subclass model on a postback.

  • 0

I’m having issues getting my controller to recognize the subclass model on a postback.

I’m creating a dynamic web form with the field meta data stored in the db. For my ViewModels, I have a two parent types

public class Form
{
    public List<Question> Questions { get; set; }
}
public class Question
{
    public string QuestionText { get; set; }
}

and subclass of Question

public class TextBoxQuestion : Question
{
    public string TextAnswer { get; set; }
    public int TextBoxWidth { get; set; }
}

I also have a view that takes a Form type as the Model and two display templates, one for Question and one form TextBoxQuestion.

//Views/Form/index.cshtml
@model Form
@Html.DisplayFor(m => m.Questions)

–

//Views/Shared/DisplayTemplates/Question.cshtml
@model Question
@if(Model is TextBoxQuestion)
{
    @Html.DisplayForModel("TextBoxQuestion")
}

–

//Views/Shared/DisplayTemplates/TextBoxQuestion.cshtml
@model TextBoxQuestion
<div>
    @Model.QuestionText
    @Html.TextBoxFor(m => m.TextAnswer)
</div>

When the page loads, my controller creates an instance of TextBoxQuestion, adds it to the Question collection and passes the Form object to the view. Everything works find and the textbox appears on the page.

But when I postback to the controller, the code doesn’t recognize the question as a TextBoxQuestion. It only sees it as the parent type Question.

[HttpPost]
public ActionResult Index(Form f)
{
    foreach (var q in f.Questions)
    {
        if (q is TextBoxQuestion)
        {
            //code never gets here
        }
        else if (q is Form)
        {
            //gets here instead
        }
    }
}

Is there something I’m missing?

  • 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-14T06:55:13+00:00Added an answer on June 14, 2026 at 6:55 am

    The model binder will create an instance of the type the method is expecting. If you want to use subclasses and display templates this way you will need to write your own model binder.

    I would suggest to check for existence of one or several attributes to determine whether a TextBoxQuestion or Question is being posted.

    Modelbinder:

    public class QuestionModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            Question result;
            ValueProviderResult text = bindingContext.ValueProvider.GetValue("TextAnswer");
            if (text != null) // TextAnswer was submitted, we'll asume the user wants to create a TextBoxAnswer
                result = new TextBoxQuestion { TextAnswer = text.AttemptedValue /* Other attributes */ };
            else
                result = new Question();
    
            // Set base class attributes
            result.QuestionText = bindingContext.ValueProvider.GetValue("QuestionText").AttemptedValue;
            return result;
        }
    }
    

    Then hook it up in Global.asax.cs:

    ModelBinders.Binders.Add(typeof(Question), new QuestionModelBinder());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.