多くのプロパティとナビゲーション フィールドを持つかなり長いモデル クラスがあります。
public class Customers
{
public Customers()
{
this.Files = new HashSet<Files>();
}
public int CustomerID { get; set; }
public int InspectionID { get; set; }
public int Position { get; set; }
public Nullable<int> Height { get; set; }
public Nullable<int> Weight{ get; set; }
//.....many more properties
//there are some navigation properties below
//.....
}
ビューを分割するために (そしてビジネス ロジックでそれが必要です)、このクラスのプロパティのサブセットを持つ約 14 の部分ビューを作成し、ページの読み込み時に対応する部分ビューをメインの編集ビューに読み込みます。コメント) 別のビューでは、リンクがクリックされると、コントローラーに渡された CustomerID パラメーターを使用して編集 GET アクションが呼び出され、コントローラーは、このような対応するパーシャルビューで編集ビューをロードします
@model myApplication.Models.CustomersViewModel //Trial 2 from below uses //this and
@model myApplication.Models.Customers //Trial 1 from below uses this
@{
ViewBag.Title = "Edit";
}
@section Styles {
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div>
@*All commong properties here*@
</div>
@*the corresponding PartialView with subset /selected/ properties here -UPDATE: please note that the partailview is named as the PK i.e. inspection id (Sorry, I worte wrongly on the initial post)
@Html.Partial("_edittemplates/" + Model.InspectionID)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
</div>
ここで、これらのプロパティのサブセットを持つ 14 個の部分ビューのそれぞれに対して 14 個の ViewModel を作成したくありません。私がやりたかったのは、1 つの大きな ViewModel を使用し、automapper を使用してビューに読み込まれるプロパティを管理し (メインと部分の両方から)、Model クラスのすべてのプロパティを複製 (コピーして貼り付け) することなく、これらの特定のプロパティをマップすることです。 CustomersViewModel に。
試行 1:
クラスをそれ自体にマップしようとしましたが、外部キーの値/データベースの関係をいじって逆火しました-エラーメッセージは、「外部キーのプロパティの1つ以上がnull不可であるため、関係を変更できませんでした.. ..」。次に、 .ForAllMembers(opt=>opt.Ignore()) を追加すると、面倒になりました。
Customers customers= db.Customers .SingleOrDefault(c => c.CustomerID == id);
Customers editcustomers = AutoMapperMappings.mapper.Map<Customers , Customers >(customers);
試行 2: では、14 個の部分ビューを作成せず、automapper を使用してすべてのプロパティをコピー アンド ペーストせずに回避する別の方法だと思ったのは、次のようなものでした。
public class CustomersViewModel
{
public Customers customers { get; set; }
}
その後、マップと逆マップ
cfg.CreateMap<CustomersViewModel, Customers>().ReverseMap();
GET コントローラー
Customers customers = db.Customers.SingleOrDefault(c => c.CustomerID == id);
CustomerEditViewModel editcustomers = AutoMapperMappings.mapper.Map<Customers, CustomersEditViewModel>(customers);
editcustomers.customers = customers;
return View(editcustomers);
次に、POST メソッド
Customers locate = db.Customers .Find(editcustomers.customers.customersID);
AutoMapperMappings.mapper.Map(editcustomers.customers, locate);
//locate = editcomponents.components;
//db.Entry(locate).State = EntityState.Modified;
db.SaveChanges();
しかし、マッピングがエラーなしでうまくいき、GET がビューをロードし、POST が変更された値を受け取ったとしても、db.SaveChanges(); 変更された値を受け取りません更新は行われません。
ただし、Model クラスからすべてのプロパティをコピーして ViewModel にコピーすると、オートマッパーによるマッピングが機能し、ビューのデータが保存されます
public class CustomersViewModel
{
public int CustomerID { get; set; }
public int InspectionID { get; set; }
public int Position { get; set; }
public Nullable<int> Height { get; set; }
public Nullable<int> Weight{ get; set; }
//...and all other properties
}
私の質問は、モデル クラスのすべてのプロパティを再度一覧表示せずに、1 つの大きな ViewModel でこれを行う方法はありますか? ご提案いただきありがとうございます。