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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:50:59+00:00 2026-05-31T16:50:59+00:00

I have two related POCOs public class Parent { public Guid Id {get; set;}

  • 0

I have two related POCOs

public class Parent
{
   public Guid Id {get; set;}
   public IList<Child> ChildProperty {get; set;}
}

public class Child
{
   public Guid Id {get; set;}
   public String Name {get; set;}
}

and I have a .cshtml Razor view with

<div>
    @{
        var children =
            new SelectList(Child.FindAll(), "Id", "Name").ToList();
    }
    @Html.LabelFor(m => m.Child)
    @Html.DropDownListFor(m => m.Child.Id, , children, "None/Unknown")
</div>

I’d like to do the following in my controller class:

[HttpPost]
public ActionResult Create(Parent parent)
{
    if (TryUpdateModel(parent))
    {
        asset.Save();
        return RedirectToAction("Index", "Parent");
    }

    return View(parent);
}

Such that if the user selects “None/Unknown”, the child value of the parent object in the controller is null but if the user selects any other value (i.e. an ID of a child object retrieved from the database), the child value of the parent object is instantiated and populated with that ID.

Basically I’m struggling with how to persist a list of possible entities across the HTTP stateless boundary such that one of the entities is properly rehydrated and assigned via the default model binder. Am I just asking for too much?

  • 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-31T16:51:00+00:00Added an answer on May 31, 2026 at 4:51 pm

    Am I just asking for too much?

    Yes, you are asking for too much.

    All that’s sent with the POST request is the ID of the selected entity. Don’t expect to get much more than that. If you want to rehydrate or whatever you should query your database. The same way you did in your GET action to populate the child collection.

    Oh and there’s a problem with your POST action => you are calling the default model binder twice.

    Here are the 2 possible patterns (personally I prefer the first but the second might be useful in some situations as well when you want to manually call the default model binder):

    [HttpPost]
    public ActionResult Create(Parent parent)
    {
        if (ModelState.IsValid)
        {
            // The model is valid
            asset.Save();
            return RedirectToAction("Index", "Parent");
        }
    
        // the model is invalid => we must redisplay the same view.
        // but for this we obviously must fill the child collection
        // which is used in the dropdown list
        parent.ChildProperty = RehydrateTheSameWayYouDidInYourGetAction();
        return View(parent);
    }
    

    or:

    [HttpPost]
    public ActionResult Create()
    {
        var parent = new Parent();
        if (TryUpdateModel(parent))
        {
            // The model is valid
            asset.Save();
            return RedirectToAction("Index", "Parent");
        }
    
        // the model is invalid => we must redisplay the same view.
        // but for this we obviously must fill the child collection
        // which is used in the dropdown list
        parent.ChildProperty = RehydrateTheSameWayYouDidInYourGetAction();
        return View(parent);
    }
    

    In your code you’ve made some mix of the two which is wrong. You are basically invoking the default model binder twice.

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

Sidebar

Related Questions

I have two related entities, say @Entity public class Book { @ManyToOne Shelf shelf;
I have two models related by a foreign key: # models.py class TestSource(models.Model): name
Let's say I have two classes, Parent and Child. These two are related via
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have two related models such as this: class PartCategory < ActiveRecord::Base has_many :part_types
I have two related questions about Web services: (1) I'm currently writing a set
I have two related (via foreignkey relation) models and created admin model for parent
I have these two related tables Client (ClientId, Name) and ClientDescription (ClientDescriptionId, (FK) ClientId,
I have two related tables in my database: Page and Tag. One Page can
I have two related ComboBoxes ( continents, and countries ). When the continents ComboBox

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.