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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:37:23+00:00 2026-05-27T17:37:23+00:00

Lets imaging the we have model: public class InheritModel { public int Id {

  • 0

Lets imaging the we have model:

public class InheritModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string OtherData { get; set; }
}

We have a controller with View, that represents this model:

private InheritModel GetAll()
{
    return new InheritModel
    {
        Name = "name1",
        Description = "decs 1",
        OtherData = "other"
    };
}

public ActionResult Index()
{
    return View(GetAll());
}

Now we can edit this in View, change some data and post in back to server:

[HttpPost]
public ActionResult Index(InheritModel model)
{
    var merged = new MergeModel();
    return View(merged.Merge(model, GetAll()));
}

What i need to do:

  • In view we have a reproduction of model
  • User change something and post
  • Merge method need to compare field-by-field posted model and previous model
  • Merge method create a new InheritModel with data that was changed in posted model, all other data should be null

Can somebody help me to make this Merge method?

UPDATE(!)

It’s not a trivial task. Approaching like:

public InheritModel Merge(InheritModel current, InheritModel orig)
{
    var result = new InheritModel();
    if (current.Id != orig.Id) 
    {
        result.Id = current.Id;
    }
}

Not applicable. It’s should be Generic solution. We have more than 200 properties in the model. And the first model is built from severeal tables from DB.

  • 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-27T17:37:24+00:00Added an answer on May 27, 2026 at 5:37 pm
    public InheritModel Merge(InheritModel current, InheritModel orig)
    {
        var result = new InheritModel();
        if (current.Id != orig.Id) 
        {
            result.Id = current.Id;
        }
        if (current.Name != orig.Name) 
        {
            result.Name = current.Name;
        }
        ... for the other properties
    
        return result;
    }
    

    Another possibility is to use reflection and loop through all properties and set their values:

    public InheritModel Merge(InheritModel current, InheritModel orig)
    {
        var result = new InheritModel();
        var properties = TypeDescriptor.GetProperties(typeof(InheritModel));
        foreach (PropertyDescriptor property in properties)
        {
            var currentValue = property.GetValue(current);
            if (currentValue != property.GetValue(orig))
            {
                property.SetValue(result, currentValue);
            }
        }
        return result;
    }
    

    Obviously this works only for 1 level nesting of properties.

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

Sidebar

Related Questions

Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
Imagine a simple Action with a post public ActionResult Unsubscribe(string contentId) { // get
I have the folowing gsp page: <g:form controller=?? action=??> <h1>Search</h1> <g:submitButton name=search value=Search/> <div
Say I have models: class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print
Lets imagine I have the same database schema as here: http://www.databaseanswers.org/data_models/driving_school/index.htm If a customer
Let's imagine that I have list of files at host1 find /path/to -name *.jpg
ok im a newbie on sessions lets imagine that we have a little login
I have a struct: struct something { int a, b, c, d; }; Is
I have a XML file like this: <Document> <Tests> <Test> <Name>A</Name> <Value>1</Value> </Test> <Test>

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.