[英]Model ID changes when passed to the HttpPost method
我正在重構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。但是,在進行更改並單擊“保存”后,將通過Toy
對象方法ActionResult Edit(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
}
是因為
[Bind(Exclude = "ToyId")]
因此,ToyID屬性實際上在綁定操作中被忽略,並且未使用隱藏值。
您可以簡單地從類中刪除它,以便ToyId
從POST操作中正確綁定。 或者,您可以通過將id
路由值復制到Toy
的ToyId
屬性中(或添加名為id
的方法參數以使其自動綁定)來在控制器方法中手動綁定它。
但是,允許綁定ID屬性等存在一些潛在的問題,而另一SO提供了一個進入此窗口的窗口: ASP.NET MVC-[Bind(Exclude =“ Id”)]的替代方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.