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

本文关键字:方法 HttpPost ID 模型 | 更新日期: 2023-09-27 17:59:30

我正在重构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操作时,视图显示良好,并且在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")]
public class Toy
{
    [ScaffoldColumn(false)]
    public int ToyId { get; set; }
    // ... other stuff here
}

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

这是因为

[Bind(Exclude = "ToyId")] 

因此,ToyID属性在绑定操作中被忽略,隐藏值也没有被使用。

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

然而,允许绑定ID属性等有一些潜在的问题,而另一个SO提供了一个了解这一点的窗口:ASP.NET MVC-[Bind(Exclude="ID")]的替代方案