これが私のシナリオです。
共通のプロパティ名を持つ 2 つのモデルを作成しました
public class SimpleModel1 { // Some Properties public string Property1 { get; set; } } public class SimpleModel2 { // Some Properties public string Property1 { get; set; } // Same name as Property1 in SimpleModel1 }
このようなビューを返すアクション(インデックスなど)でSimpleModel1を使用しました
@model MvcApplication2.Models.SimpleModel1 @{ ViewBag.Title = "Home Page"; } @using (Html.BeginForm("Test", "Home", FormMethod.Post)) { <label>Enter something here</label> @Html.TextBoxFor(m => m.Property1) <button type="submit">Submit</button> }
SimpleModel1 をパラメーターとして受け取り、いくつかの作業を行い、SimpleModel2 を受け取るビューを返す Test アクションに値を送信しました。
public ActionResult Test(SimpleModel1 model) { SimpleModel2 newModel = new SimpleModel2(); // Do Something newModel.Property1 = "Something different than model's Property1"; return View(newModel); }
Test.cshtml (テスト アクションによって返されるビュー) は次のとおりです。
@model MvcApplication2.Models.SimpleModel2 @{ ViewBag.Title = "Test"; } <h2>Test</h2> @* Model Propery without using HTML extension*@ @Model.Property1 @* Model property using HTML extension (Incorrect) *@ @Html.TextBoxFor(m => m.Property1) @Html.HiddenFor(m => m.Property1) @Html.TextAreaFor(m => m.Property1) @* Correct Value *@ <input value="@Model.Property1" />
私が期待しているのは、Property1 のすべての値が、テスト アクションで設定されているように「モデルの Property1 とは異なるもの」になることです。しかし、Html 拡張子を使用するもの (Html.TextBoxFor、Html.HiddenFor など) には、Test Action に投稿された Property1 値があることが判明しました。たとえば、Test Action に「なんてびっくり」(SimpleModel1 の Property1) を投稿すると、SimpleModel2 の Property1 の値も、何を設定しても「びっくり」になります。
何が起こっているのかわかりません。私にはバグのように見えます。誰にもアイデアはありますか?