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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:08:13+00:00 2026-05-23T08:08:13+00:00

I am refactoring MvcMusicStore code and I’m in the process of updating the StoreManagerController.

  • 0

I am refactoring MvcMusicStore code and I’m in the process of updating the StoreManagerController. I have changed the Edit actions to the following:

    //
    // GET: /StoreManager/Edit/5

    public ActionResult Edit(int id)
    {
        Toy toy = dbStore.Toys.Find(id);
        ViewBag.CategoryId = new SelectList(dbStore.Categories, "CategoryId", "Name", toy.CategoryId);
        ViewBag.BrandId = new SelectList(dbStore.Brands, "BrandId", "Name", toy.BrandId);
        return View(toy);
    }

    //
    // POST: /StoreManager/Edit/5

    [HttpPost]
    public ActionResult Edit(Toy toy)
    {
        if (ModelState.IsValid)
        {
            dbStore.Entry(toy).State = EntityState.Modified;
            dbStore.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.CategoryId = new SelectList(dbStore.Categories, "CategoryId", "Name", toy.CategoryId);
        ViewBag.BrandId = new SelectList(dbStore.Brands, "BrandId", "Name", toy.BrandId);
        return View(toy);
    }

While testing, when I click the Edit action, the view displays fine and setting a breakpoint in the Edit(int id) method shows that the ToyId is 1. However, after I make a change and click Save, the Toy object being passed through the method ActionResult Edit(Toy toy) has the incorrect ToyId equal to 0.

Where is this modification occurring, or is there a copy of toy being passed back from the view and it is not correctly copying across the ToyId?

Update: Added Post of Edit View

@model Store.Models.Toy

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">      </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"   type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Toy</legend>

    @Html.HiddenFor(model => model.ToyId)

    <div class="editor-label">
        @Html.LabelFor(model => model.CategoryId, "Category")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CategoryId", String.Empty)
        @Html.ValidationMessageFor(model => model.CategoryId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.BrandId, "Brand")
    </div>
    <div class="editor-field">
        @Html.DropDownList("BrandId", String.Empty)
        @Html.ValidationMessageFor(model => model.BrandId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Price)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Price)
        @Html.ValidationMessageFor(model => model.Price)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PictureUrl)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PictureUrl)
        @Html.ValidationMessageFor(model => model.PictureUrl)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Toy Class

[Bind(Exclude = "ToyId")]
public class Toy
{
    [ScaffoldColumn(false)]
    public int ToyId { get; set; }

    // ... other stuff here
}
  • 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-23T08:08:13+00:00Added an answer on May 23, 2026 at 8:08 am

    It’s because of

    [Bind(Exclude = "ToyId")] 
    

    So the ToyID property is literally being ignored in the bind operation and the hidden value is not being used.

    You can simply remove this from the class so that ToyId gets correctly bound from the POST operation; or, you can manually bind it in the controller method by copying the id route value to the Toy‘s ToyId property (or add a method parameter called id to have it bound automatically).

    There are some potential issues with allowing binding of ID properties etc, however, and this other SO provides a window into this: ASP.NET MVC – Alternative for [Bind(Exclude = "Id")]

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

Sidebar

Related Questions

I'm currently refactoring some Javascript code we have and amongst other things I've changed
While refactoring some old code I have stripped out a number of public methods
I am currently refactoring some code which performs Windows Impersonation for testability and have
Recently I have been refactoring some of my C# code and I found a
Whilst refactoring code I changed the all the if not null conditions to follow
While refactoring my code base I found a piece of code which I'd like
I'm refactoring some code and would like to junit test some methods but they
I am refactoring some code where I grab the values inputted into textboxes and
I was refactoring some code and found there are two places that can be
Im refactoring a class, and moving sections into a base class. I have a

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.