2011-06-20 9 views
0

MvcMusicStoreコードをリファクタリングしていますが、StoreManagerControllerを更新中です。私は、次の編集操作が変更されました:HttpPostメソッドに渡すとモデルIDが変更されます

// 
    // 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オブジェクトがビューから戻って渡されるtoyのコピーをどこでこの変更が発生している0

に等しい間違ったToyIdを持っている、または存在しActionResult Edit(Toy 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 
} 
+0

あなたは「編集」ビューを投稿することができますか? –

+0

...そして、 'Toy'のクラス本体 - または少なくとも' ToyId'メンバーを宣言するビット –

+0

- 私はちょうどmvcmusicstoreをリファクタリングしています。 – Seth

答えて

1

それはそうToyIDプロパティは文字通りignoされているため

[Bind(Exclude = "ToyId")] 

のです赤色になり、隠し値は使用されていません。

これを単にクラスから削除して、ToyIdがPOST操作から正しくバインドされるようにすることができます。またはコントローラメソッドでidルート値をのToyIdプロパティにコピーするか、または自動的にバインドされるようにidというメソッドパラメータを追加してコントローラメソッドで手動でバインドできます。

ありしかし、などのIDプロパティの結合を許可すると、いくつかの潜在的な問題があり、この他には、SOこれにウィンドウを提供します:ASP.NET MVC - Alternative for [Bind(Exclude = "Id")]

+0

これはmvcmusicstoreのバグだったようです:http://mvcmusicstore.codeplex.com/workitem/6648?PendingVoteId=6648 – Seth

+0

@セス - あなたはあまりチャンスがありませんでした。 –

関連する問題