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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:57:01+00:00 2026-05-22T15:57:01+00:00

I have a simplified test scenario useful for asking this question: A Product can

  • 0

I have a simplified test scenario useful for asking this question: A Product can have many Components, a Component can belong to many Products. EF generated the classes, I’ve slimmed them as follows:

public partial class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Component> Components { get; set; }
}
public partial class Component
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

The creation of a component is accomplished via these controller actions:

public ActionResult Create(int ProductId)
{
    Product p = db.Products.Find(ProductId);
    Component c = new Component();
    c.Products.Add(p);
    return PartialView(c);
} 

[HttpPost]
public ActionResult Create(Component model)
{
    db.Components.Add(model);
    db.SaveChanges();
}

and the view returned by the GET method looks like this:

@model Test.Models.Product

<fieldset>
    <legend>Product</legend>
    <div class="display-label">Name</div>
    <div class="display-field">@Model.Name</div>
</fieldset>

@Html.Action("Create", "Component", new {ProductId = Model.Id}) 
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.Id }) |
    @Html.ActionLink("Back to List", "Index")
</p>

From which can be seen that the component creation is handled on the same page via the above Html.Action – the code for that view follows:

@model Test.Models.Component
@using Test.Models

<script type="text/javascript">
    function Success() {
        alert('ok');
    }
    function Failure() {
        alert('err');
    }
</script>
@using (Ajax.BeginForm("Create", "Component", new AjaxOptions
{
    HttpMethod = "Post",
    OnSuccess = "Success",
    OnFailure = "Failure"
}))
{
    <fieldset>
        <legend>Components</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        @Html.HiddenFor(x => x.Products.First().Id)
        @Html.HiddenFor(x => x.Products)
        @foreach (Product p in Model.Products)
        {
            @Html.Hidden("Products[0].Id", p.Id)
        }
        @foreach (Product p in Model.Products)
        {
            @Html.Hidden("[0].Id", p.Id)
        }
    </fieldset>
    <input type="submit" value="go" />
}

ok. so this is what I’m struggling with: I need the model parameter of the [HttpPost]back to get properly populated i.e. it should contain a Product, since I can’t create the new component with a null product. To get the product I need to look it up via the product’s id. I expect I should be able to do:

model.Products.Add(db.Products.Find(model.Products.First().Id));

or some such thing, which relies on model receiving the id. This means the view has to place the id there, presumably in a hidden field, and as can be seen from my view code, I’ve made several attempts at populating this, all of which have failed.

Normally I prefer the *For methods since they become responsible for generating correct nomenclature. If .Products were singular (.Product), I could reference it as x => x.Product.Id and everything would be fine, but since it’s plural, I can’t do x => x.Products.Id so I tried x => x.Products.First().Id which compiles and produces the right value but gets name Id (which is wrong since the model binder thinks it’s Component.Id and not Component.Products[0].Id.

My second attempt was to let HiddenFor iterate (like I would with EditorFor):

@Html.HiddenFor(x => x.Products)

but that produces nothing – I’ve read that this helper doesn’t iterate. I tried x => x.Products.First() but that doesn’t even compile. Finally, I decided to abandon the *For and code the name myself:

@foreach (Product p in Model.Products)
{
    @Html.Hidden("Products[0].Id", p.Id)

and though that looks right, the postback doesn’t see my value (Products.Count == 0). I saw in some posting that format should look like [0].Id but that doesn’t work either. grr…

I gather I could code it like this:

@Html.Hidden("ProductId", p.Id)

and then redeclare my controller action like this:

[HttpPost] ActionResult Create(Component model, int ProductId)

but that seems eecky. it’s hard to believe this is so difficult. can anyone help?

  • e

p.s. I have a project I could make available for download if anyone cares

  • 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-22T15:57:02+00:00Added an answer on May 22, 2026 at 3:57 pm

    Instead of writing those foreach loops try using editor templates:

    <fieldset>
        <legend>Components</legend>
    
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
    
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    
        @Html.EditorFor(x => x.Products)
    </fieldset>
    

    and inside the corresponding editor template (~/Views/Shared/EditorTemplates/Product.cshtml)

    @model Product
    @Html.HiddenFor(x => x.Id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables which in simplified form look like this: Products( id: int,
I have a regular expression like this (much simplified): ^(ab)*$ And am matching against
I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
The scenario is as follows, I have 3 objects (i simplified the names) named
I have a class, which I have simplified to this: final class Thing {
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have a simplified ajay script, from which I have removed all nonrelevant code.
I have a linq to sql database. Very simplified we have 3 tables, Projects
I have a database where I store objects. I have the following (simplified) schema
I have a virtual host setup on Apache 2 like so (simplified): <VirtualHost *>

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.