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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:50:27+00:00 2026-06-15T04:50:27+00:00

My Problem Source object Properties of the same class that do not exist in

  • 0

My Problem

“Source” object Properties of the same class that do not exist in the View, are overwritting the same properties in the “Target” object with nulls. How do I prevent this? In affect how do I ensure only populated(not null) properties are merged into the “Target” object. I have also tried this with Automapper and failed, but I would be happy with an Automapper solution as an alternative.

I do appreciate that this “Null Mapping” question has appeared before, but I fear my situation is more involved since there are nested objects. Well I tried the suggested options and I could not get them to work. So here I am.

Huge gratitude for any help.

I appreciate this is a complex problem, and really, really appreciate any help with this, particularly if someone can produce a code sample for me. I have pulling my hair out over this for a few days 🙁

What I have attempted

I have 2 objects, one is the original(“Target”), one(“Source”) is populated by a form ie a View.

The Original “Target” Object(myOrigDoc) looks like this:

enter image description here

The Form “Source” Object(myDoc) looks like this:

enter image description here

I then do the mapping:

            myOrigDoc.Introduction.InjectFrom<StrNotNull>(myDoc.Introduction);

using the following injector:

    public class StrNotNull: ConventionInjection
{
    bool blnNotNull = false;
    bool blnMatch = false;
    protected override bool Match(ConventionInfo c)
    {
        blnNotNull = false;
        blnMatch = false;

        //if ((c.SourceProp.Type == typeof(string)) && (c.SourceProp.Value != null))
        //    blnAssignable = true;

        if (c.SourceProp.Value != null)
            blnNotNull = true;

        if ((c.SourceProp.Name == c.TargetProp.Name) && (blnNotNull)) 
            blnMatch = true;

        return blnMatch;
    }
}

and I end up with:

enter image description here

The Form has no “DateOfBirth” field on it, therefore I suspect Model Binding is creating a null value for the “DataOfBirth” property, on the new “MyDoc” object, when I call:

        public ActionResult Index(Document myDoc)

Many thanks, Ed.

EDIT1: I believe this is a nested mapping problem due to the subclasses. Not sure how I sort this in ValueInjector.

EDIT2: Possible Automapper Solution from documentation for nested mappings, but I could not get it to work. I still get my nulls going across into the target.:

Mapper.CreateMap<XSD_Smart2.Document, XSD_Smart2.Document> 
().ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

Mapper.CreateMap<XSD_Smart2.DocumentIntroduction, XSD_Smart2.DocumentIntroduction>  
().ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

Mapper.CreateMap<XSD_Smart2.Client, XSD_Smart2.Client>().ForAllMembers(opt => 
opt.Condition(srs => !srs.IsSourceValueNull));
  • 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-15T04:50:29+00:00Added an answer on June 15, 2026 at 4:50 am

    Update for ValueInjecter 3

    public class IgnoreNulls : LoopInjection
    {
        protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
        {
            var val = sp.GetValue(source);
            if (val != null)
            {
                tp.SetValue(target, val);
            }
        }
    }
    

    previous version

    create a custom injection that will have this behaviour:

        public class IgnoreNulls : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == c.TargetProp.Name
                      && c.SourceProp.Value != null;
            }
        }
    

    and use it:

        target.InjectFrom<IgnoreNulls>(source);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found the source of my problem for SuperAgent (http://visionmedia.github.com/superagent/) on Firefox. Not sure
well this is my problem i use 2 source first query (select * from
I have this problem: I want to generate a new source code file from
This is the complete source code: http://www.sendspace.com/file/lwxpyf I have a problem with a JSF
I am trying to create a pageing and sorting object data source that before
I have an object called TestData that contains two properties, a string and an
I need help with the following problem: I have a class with two properties.
this is my problem i have a DataGrid and this his source var Query
I have a problem with some source code regarding OpenSSL and Cpp. For some
I have a problem with 2D transformation program I have the source code import

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.