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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:37:29+00:00 2026-06-09T05:37:29+00:00

I’m having trouble displaying and saving navigation properties. I have 3 tables: RateProfile RateProfileID

  • 0

I’m having trouble displaying and saving navigation properties.

I have 3 tables:

RateProfile

  • RateProfileID
  • ProfileName

Rate

  • RateID
  • RateProfileID
  • PanelID
  • amount

Panel

  • PanelID
  • PanelName

I have models for each of these and navigation properties for foreign keys (i.e. Rates is a navigation property of RateProfile). I have an edit page with RateProfile as the model. On this page, I have a dropdown for PanelID. The dropdown filters which navigation properties are shown (each panelID has 28 rates associated with it). Whenever the dropdown is changed, it posts to my Edit method (to save the existing data before moving on and to display the rates for the new panelID selected).

I pass only the needed rates records to my view a List via the ViewBag.

I was initially using a foreach loop

foreach(PDR.Models.Rate rate in ViewBag.Rates){
                    @Html.HiddenFor(modelItem => rate.RateProfileID)
                    @Html.HiddenFor(modelItem => rate.RateID)
                    @Html.HiddenFor(modelItem => rate.PanelID)

                    @Html.EditorFor(modelItem => rate.Amount)
                    @Html.ValidationMessageFor(modelItem => rate.Amount)
}

With a foreach loop, everything displayed properly when changing the dropdown for PanelID, ie it would display with the correct rates for each panel. However, none of my rates would get passed along with my Model back to my HTTPPost Edit method in my controller. The RateProfile passed to my controller would have 0 records in it’s Rates navigation property (there were 28 in the view).

I was advised to change the foreach loop into a for(int i…) loop. Apparently foreach loops name all of the textboxes the same thing and a regular for loop names them based on the index.

Using a for(int i…) loop, the Rates navigation property did indeed have all of it’s records when passed to the HTTPPost Edit method and I was able to update those records based on what I entered.

However… now with the for(int i…) loop, the page will not display the correct records based off of panelID (it always displays records for the default panelID even when changed). What’s even crazier is that if I put break points in my view, I see that the correct Rate records are being passed to the View still. I see that the correct RateID/amount etc are all being put into the Html helpers, but when the page actually displays, it displays the data that was there before changing the dropdown rather than the correct data that I was watching it populate in debug mode.

Here’s the view code that am I using since switching from a foreach loop to a normal for loop:

    @{List<PDR.Models.Rate> rates = ViewBag.rates;
      var count = rates.Count;
      }
    @for (int i = 0; i < count; i++)
    {
                    @Html.HiddenFor(modelItem => rates[i].RateProfileID)
                    @Html.HiddenFor(modelItem => rates[i].RateID)
                    @Html.HiddenFor(modelItem => rates[i].PanelID)

                    @Html.EditorFor(modelItem => rates[i].Amount)
                    @Html.ValidationMessageFor(modelItem => rates[i].Amount)
    }

From checking values in debug mode in my view, I know that the correct rates are being passed to ViewBag.rates based on panelID. The problem is that even though I can step through it in debug and watch it put the correct values for the RateID,PanelID, and Amount for each rate in the above loop, what actually displays on the page is always the values for panelID == 1, and then the values passed to my controller are always the values for the rates where panelID == 1 rather than the rates that I passed to the view.

Here’s the code for the edit method. I’m pretty sure the problem is in the view though, because the rateprofile (which should be the model from my View) is never getting the correct navigation properties back in the first place.

[HttpPost]
    public ActionResult Edit(RateProfile rateprofile, int panelID)
    {

        var panels = db.Panels;
        ViewBag.PanelDropDown = new SelectList(panels, "PanelID", "PanelName", panelID);
        ViewBag.PanelID = panelID;

        if (ModelState.IsValid)
        {
            db.Entry(rateprofile).State = EntityState.Modified;

            foreach (Rate rate in rateprofile.Rates)
            {
                db.Entry(rate).State = EntityState.Modified;
            }
            db.SaveChanges();
        }
        rateprofile = db.RateProfiles.Include(a => a.Rates).Where(a => a.RateProfileID == rateprofile.RateProfileID).Single(); //reloads the current rateprofile from the db
        PopulatePanelDropDown(rateprofile, panelID); //populates the ViewBag.rates variable

        return View(rateprofile);
    }

In summary:

  • foreach loop over Rates: displays everything properly based on panelID, but will not pass values to controller.

  • for(int i…) loop: Will only display values for panelID == 1 and will not update based on panelID. Will save, but only the records associated with panelID == 1.

  • 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-09T05:37:30+00:00Added an answer on June 9, 2026 at 5:37 am

    After searching around for several weeks and not finding any answers that worked for me, I finally just changed my HTTPpost Edit Method to Redirect to my HTTPget Edit method instead of returning the view. I pass my RateProfileID and the panelID to my HTTPget Edit Method and it doesn’t have any problem showing the correct data now when the panel drop down is changed.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.