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

  • Home
  • SEARCH
  • 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 483831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:14:15+00:00 2026-05-13T01:14:15+00:00

I have a pair of views in my application that both display the same

  • 0

I have a pair of views in my application that both display the same Editor Template for one of my model items; of the two views (“Add” and “Edit”), “Edit” works fine, but “Add” is returning null for the model when my controller action handles the post.

I found that if I give the “Add” view a custom ViewModel and call Html.EditorFor(p => p.PageContent) rather than simply calling the EditorFor() on the whole Model object- Html.EditorFor(p => p), then the form returns the correct, non-null model, but that generates other problems pertaining to my client-side scripting and control IDs (as now all of the fields are prefixed with “PageContent_”). I am using the same Editor Template technique in a few different places throughout my application and none of the others are exhibiting this odd dependency on a ViewModel.

Has anyone else ever experienced similar problems?

Edit View

<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/Admin/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PageContent>" %>

<% using (Html.BeginForm())
   { %>
<%=Html.Hidden("PageID", Model.Page.ID) %>
<%=Html.EditorFor(p => p)%>
<input type="submit" name="btnSave" value="Save" />
<input type="submit" name="btnCancel" value="Cancel" class="cancel" />
<% }

Action (Working)

[HttpPost, ValidateInput(false)]
public ActionResult EditContent(int id, FormCollection formCollection) {}

Add View

<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/Admin/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PageContent>" %>

<% using (Html.BeginForm())
   { %>
<%=Html.Hidden("PageID", ViewData["PageID"]) %>
<%=Html.EditorFor(p => p)%>
<input type="submit" name="btnSave" value="Save" />
<input type="submit" name="btnCancel" value="Cancel" class="cancel" />
<% } %>

Action (Failing)

// content is ALWAYS null
[HttpPost, ValidateInput(false)]
public ActionResult AddContent(PageContent content, FormCollection formCollection) {}

Before you cry “duplicate”

This question does relate to this one, but this question is intended to target the specific problem I am experiencing rather than the more general question asked there.

  • 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-13T01:14:15+00:00Added an answer on May 13, 2026 at 1:14 am

    I tracked down the problem and it’s a rather interesting one.

    When the DefaultModelBinder attempts to resolve a model item one of the first things it does is check to see if there are any prefixed fields in the data being bound; it does this by checking for any form items that begin with the name of the model object (this seems extremely arbitrary, if you ask me). If any “prefixed” fields are found then it results in different binding logic being invoked.

    ASP.NET MVC 2 Preview 2 BindModel() Source

    public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
        if (bindingContext == null) {
            throw new ArgumentNullException("bindingContext");
        }
        bool performedFallback = false;
        if (!String.IsNullOrEmpty(bindingContext.ModelName) && !DictionaryHelpers.DoesAnyKeyHavePrefix(bindingContext.ValueProvider, bindingContext.ModelName)) {
            // We couldn't find any entry that began with the prefix. If this is the top-level element, fall back
            // to the empty prefix.
            if (bindingContext.FallbackToEmptyPrefix) {
                 /* omitted for brevity */
                };
                performedFallback = true;
            }
            else {
                return null;
            }
        }
    
        // Simple model = int, string, etc.; determined by calling TypeConverter.CanConvertFrom(typeof(string))
        // or by seeing if a value in the request exactly matches the name of the model we're binding.
        // Complex type = everything else.
        if (!performedFallback) {
           /* omitted for brevity */
        }
        if (!bindingContext.ModelMetadata.IsComplexType) {
            return null;
        }
        return BindComplexModel(controllerContext, bindingContext);
    }
    

    The controller action I defined to handle the Add action defines a PageContent item called “content” and in my domain PageContent has a property called “Content” which “matched” with the model name of “content” thus causing the DefaultModelBinder to assume I had a prefixed value when in fact it was simply a member of PageContent. By changing the signature-

    from:

    [HttpPost, ValidateInput(false)]
    public ActionResult AddContent(PageContent content, FormCollection formCollection) {}
    

    to:

    [HttpPost, ValidateInput(false)]
    public ActionResult AddContent(PageContent pageContent, FormCollection formCollection) {}
    

    The DefaultModelBinder was once again able to correctly bind to my PageContent model item. I’m not sure why the Edit view didn’t also display this behavior, but either way I’ve tracked down the source of the issue.

    It seems to me that this issue falls very close to “bug” status. It makes sense that my view worked initially with the ViewModel because “content” was getting prefixed with “PageContent_”, but a core framework feature/bug like this ought not be unaddressed IMHO.

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

Sidebar

Related Questions

Working with the MVVM pattern, I have a pair of view model classes that
If I have a pair of functions that both set local variables, for example,
I have a pair of static fields with a complicated one-time initialization. I want
I have created a server/client application. Both of them are written in C#. It
I have a pair of Django models with a foreign key, one of which
I have a model that includes attributes like company_name, posted (a date), location (lat/lng
I'm using MVC3 w/ Razor and I have a model that has quite a
Hi I have a simple application where I have a some custom views and
i have a pair of radio buttons that upon certain circumstances will be enabled
I have a table that logs page views of each user: +--------------+--------------+------+-----+---------+----------------+ | Field

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.