C# 模型ID在传递给HttpPost方法时发生更改

C# 模型ID在传递给HttpPost方法时发生更改,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我正在重构MvcMusicStore代码,并且正在更新StoreManagerController。我已将编辑操作更改为以下内容: // // GET: /StoreManager/Edit/5 public ActionResult Edit(int id) { Toy toy = dbStore.Toys.Find(id); ViewBag.CategoryId = new SelectList(dbStore.Categor

我正在重构MvcMusicStore代码,并且正在更新StoreManagerController。我已将编辑操作更改为以下内容:

    //
    // 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);
    }
测试时,当我单击
Edit
操作时,视图显示fine,在
Edit(int-id)
方法中设置断点显示
ToyId
为1。但是,在我进行更改并单击“保存”后,通过方法
ActionResult Edit(Toy Toy)
传递的
Toy
对象的
ToyId
值不正确,等于0

该修改发生在何处,或者是否存在从视图传回的
toy
副本,并且它没有正确地跨ToyId复制

更新:添加了编辑视图的帖子

@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>
是因为

[Bind(Exclude = "ToyId")] 
因此,ToyID属性实际上在绑定操作中被忽略,而隐藏值没有被使用

您可以简单地从类中删除它,以便从POST操作中正确绑定
ToyId
;或者,您可以通过将
id
路由值复制到
Toy
ToyId
属性,在控制器方法中手动绑定它(或者添加名为
id
的方法参数以自动绑定它)


但是,允许绑定ID属性等存在一些潜在的问题,而另一个问题则提供了一个窗口:您可以发布您的“编辑”吗视图?…和
Toy
的类主体-或者至少声明
ToyId
成员的位完成了-我刚刚重构了mvcmusicstore。似乎这是mvcmusicstore的一个bug:@Seth-那么,你没有太多机会了:D
[Bind(Exclude = "ToyId")]