0

私はデータベースの最初のメソッドを使用しています。私は、データが入力される単一のフォームを持っています。これにより、新しい「フォーム入力」、新しい顧客と3つの新しいアドレスが作成されます。 2つのアドレスは顧客に関連付けられ、1つのアドレスはフォームエントリ全体に関連付けられ、顧客はフォームエントリに関連付けられます。ASP.NET MVC EntityFramework作成時に複数の子オブジェクトとともに新しいオブジェクトを保存する方法

質問(改訂2): 1は通常、同時に作成されるオブジェクトを参照するフィールドの作成に取り掛かるんどのように - 「ねえ、右ここではこれらのidフィールドが同じで同時に作成されたオブジェクトを参照しますあなたが構築している現在のデータセット "?

すなわち1が正常にMVCで、次のようなものだろう方法:

1)新しいアドレスとして3つのアドレスのそれぞれを作成します。

2)checkoutForm.Customer.HomeAddressIdとcheckoutForm.Customer.MailingAddressIdを使用して2人を新しい顧客に関連付けます。

3)もう一方をcheckoutForm.PlaceOfUseAddressId自体に関連付けます。

4)顧客を保存します。

5)customerをcheckoutForm.CustomerIdに関連付けます。その間にModelState.IsValidが偽なっていない間checkoutForm.CustomerIdと各x.AddressIdが必要ですが、最初はnullをされているので、最後

6)、

をcheckoutForm自身を救いますか?

同時に、このような複数の依存オブジェクトを作成するための手順を説明し、何かへの参照は素晴らしいだろう!


編集:作成のために私は()のパラメータからbind属性を削除し、そしてそれは、すべてのデータが正しいフィールドに引きます。ドロップダウンリストはそれぞれのStateIdを適切に取得しています。

現在、ModelState.IsValidは送信時にfalseを報告します。子オブジェクトの必須のIDはnullです(これは意味があります。これは.NETに伝える方法を理解しようとしているためです - データを取得するとオブジェクトの関連付けが行われます)。


EDIT2:ModelState.IsValid == falseを、checkoutFormおよび顧客のための適切なIDを参照することので:私は今、私は私が実際に修正するために必要なものに近づいてることを、まだ再び質問を洗練依存関係はnullです。このフォームの背後にあるモデルの


描写:ここ Entity Framework model diagram

はフォーム自体です:。 Page containing form

注2 dropdownlists他のすべてが、文字列、int型、またはのためのテキストボックスがあります日付。

これまでコントローラ用に用意していたもの(足場が作り出したものをはるかに超えたもの):

// GET: CheckoutForms/Create 
public ActionResult Create() 
{ 
    ViewBag.HomeStateList = new SelectList(db.RT_STATE_LIST, "ST_SEQ", "ST_ABBR", 2); 
    ViewBag.MailingStateList = new SelectList(db.RT_STATE_LIST, "ST_SEQ", "ST_ABBR", 2); 
    return View(); 
} 

// POST: CheckoutForms/Create 
[HttpPost] 
[ValidateAntiForgeryToken] 
// public ActionResult Create([Bind(Include = "Customer.FirstName,Customer.LastName,VacuumNumber,Customer.Phone,Customer.DriversLicense,Customer.HomeAddress.Street,Customer.HomeAddress.City,Customer.HomeAddress.StateId,Customer.MailingAddress.Street,Customer.MailingAddress.City,Customer.MailingAddress.StateId,Customer.MailingAddress.PostalCode,PlaceOfUseAddress.Street,PlaceOfUseAddress.City,PlaceOfUseAddress.StateId,PickupDate,DueDate,ReturnedDate,EnteredBy,EnteredDate,ModifiedBy,ModifiedDate")] CheckoutForm checkoutForm) 
public ActionResult Create(CheckoutForm checkoutForm) 
{ 
    if (ModelState.IsValid) 
    { 
      checkoutForm.PlaceOfUseAddress.StateId = 1; 

      // !!! Need to be given correct value once authorization is set up. 
      checkoutForm.EnteredBy = -1; 
      checkoutForm.ModifiedBy = -1; 
      checkoutForm.EnteredDate = System.DateTime.Now; 
      checkoutForm.ModifiedDate = System.DateTime.Now; 

      // ??? db.Addresses.Add(checkoutForm.PlaceOfUseAddress); 
      // ??? db.Addresses.Add(checkoutForm.Customer.HomeAddress); 
      // ??? db.Addresses.Add(checkoutForm.Customer.MailingAddress); 
      // ??? db.SaveChanges(); 
      // ??? checkoutForm.PlaceOfUseAddressId = checkoutForm.PlaceOfUseAddress.AddressId; 
      // ??? checkoutForm.Customer.HomeAddressId = checkoutForm.Customer.HomeAddress.AddressId; 
      // ??? checkoutForm.Customer.MailingAddressId = checkoutForm.Customer.MailingAddress.AddressId; 
      // ??? db.Customers.Add(checkoutForm.Customer); 
      // ??? db.SaveChanges(); 
      db.CheckoutForms.Add(checkoutForm); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

     ViewBag.HomeStateList = new SelectList(db.RT_STATE_LIST, "ST_SEQ", "ST_ABBR", checkoutForm.Customer.HomeAddress.StateId); 
     ViewBag.MailingStateList = new SelectList(db.RT_STATE_LIST, "ST_SEQ", "ST_ABBR", checkoutForm.Customer.MailingAddress.StateId); 
     return View(checkoutForm); 
} 

最後に、ここに取り除か他のすべてとのビューからフォーム要素は、(次のとおりです。

@using (Html.BeginForm()) 
{ 
    @Html.HiddenFor(model => model.CustomerId) 
    @Html.HiddenFor(model => model.PlaceOfUseAddressId) 
    @Html.HiddenFor(model => model.Customer.HomeAddressId) 
    @Html.HiddenFor(model => model.Customer.MailingAddressId) 
    @Html.HiddenFor(model => model.EnteredBy) 
    @Html.HiddenFor(model => model.EnteredDate) 
    @Html.HiddenFor(model => model.ModifiedBy) 
    @Html.HiddenFor(model => model.ModifiedDate) 


    <!-- Name and Vacuum Number --> 
    @Html.EditorFor(model => model.Customer.FirstName, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.Customer.LastName, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.VacuumNumber, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    <!-- Driver's License --> 
    <!-- Phone Number --> 
    @Html.EditorFor(model => model.Customer.Phone, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.Customer.DriversLicense, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    <!-- Place of Use Address --> 
    @Html.EditorFor(model => model.PlaceOfUseAddress.Street, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.PlaceOfUseAddress.City, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    <!-- Customer's Home Address --> 
    @Html.EditorFor(model => model.Customer.HomeAddress.Street, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.Customer.HomeAddress.City, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.DropDownListFor(model => model.Customer.HomeAddress.StateId, ViewBag.HomeStateList as SelectList, htmlAttributes: new { @class = "form-control checkout" }) 

    <!-- Customer's Mailing Address --> 
    @Html.EditorFor(model => model.Customer.MailingAddress.Street, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.Customer.MailingAddress.City, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.DropDownListFor(model => model.Customer.MailingAddress.StateId, ViewBag.MailingStateList as SelectList, htmlAttributes: new { @class = "form-control checkout" }) 

    @Html.EditorFor(model => model.Customer.MailingAddress.PostalCode, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    <!-- Dates Picked Up, Due, and Returned --> 
    @Html.EditorFor(model => model.PickupDate, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.DueDate, new { htmlAttributes = new { @class = "form-control checkout" } }) 

    @Html.EditorFor(model => model.ReturnedDate, new { htmlAttributes = new { @class = "form-control checkout" } }) 
} 
+1

その[ひどい[Bind]]属性を取り除き、データを編集するためのビューモデルを使用します(フラットな構造で、あなたの 'SelectLists'のプロパティを含む) –

+0

MVVMのように"モデルを表示する "を意味しますか?または、MVCのモデル(明らかに)で見つかるクラスのタイプですか? – GG2

+0

投稿ごとに1つの尖った質問をお願いします。そうすれば、尖った答えを得る機会が増えます。 –

答えて

0

はそれを考え出しました。

ビュー

、隠しフィールドは外部キーアドレスIDS(私はそれらを持っていたとして)に接続されている、だけでなく、偽の値が事前なければなりません(そう、彼らはnullではありません)ので、のように、:

@Html.HiddenFor(model => model.CustomerId, new { @Value = "0" }) 
@Html.HiddenFor(model => model.PlaceOfUseAddressId, new { @Value = "0"}) 
@Html.HiddenFor(model => model.Customer.HomeAddressId, new { @Value = "0" }) 
@Html.HiddenFor(model => model.Customer.MailingAddressId, new { @Value = "0" }) 

次に、MVCが正常にすべての部分を処理するため、コントローラの手順はかなり簡単になります。私はちょうど上の子供、それらを保存して関連付ける必要があります。

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(CheckoutForm checkoutForm) 
{ 
    Address PlaceOfUse = new Address(); 
    Address HomeAddy = new Address(); 
    Address MailingAddy = new Address(); 
    Customer Client = new Customer(); 

    if (ModelState.IsValid) 
    { 
     // Not currently available to the user to be filled in by them 
     checkoutForm.PlaceOfUseAddress.StateId = 1; 

     // !!! Need to be given correct value once authorization is set up. 
     checkoutForm.EnteredBy = TestAccountId; 
     checkoutForm.ModifiedBy = TestAccountId; 
     checkoutForm.EnteredDate = System.DateTime.Now; 
     checkoutForm.ModifiedDate = System.DateTime.Now; 

     PlaceOfUse = checkoutForm.PlaceOfUseAddress; 
     HomeAddy = checkoutForm.Customer.HomeAddress; 
     MailingAddy = checkoutForm.Customer.MailingAddress; 
     db.Addresses.Add(PlaceOfUse); 
     db.Addresses.Add(HomeAddy); 
     db.Addresses.Add(MailingAddy); 
     db.SaveChanges(); 
     checkoutForm.PlaceOfUseAddressId = PlaceOfUse.AddressId; 
     checkoutForm.Customer.HomeAddressId = HomeAddy.AddressId; 
     checkoutForm.Customer.MailingAddressId = MailingAddy.AddressId; 

     Client = checkoutForm.Customer; 
     db.Customers.Add(Client); 
     db.SaveChanges(); 
     checkoutForm.CustomerId = Client.CustomerId; 
     db.CheckoutForms.Add(checkoutForm); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    else { 
     // To review in debug mode, when there are problems 
     //var errors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors); 

     ViewBag.HomeStateList = new SelectList(db.RT_STATE_LIST, "ST_SEQ", "ST_ABBR", checkoutForm.Customer.HomeAddress.StateId); 
     ViewBag.MailingStateList = new SelectList(db.RT_STATE_LIST, "ST_SEQ", "ST_ABBR", checkoutForm.Customer.MailingAddress.StateId); 
     return View(checkoutForm); 
    } 
} 

作成します()のパラメータから「バインド」属性を削除Plusは、それはすべての非常に簡単に作られました。

+0

また、編集で複雑なオブジェクト関係のこれらの種類を更新する方法に関する以下の優れた関連情報があることについてもお話したいと思います。http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-記録 – GG2

関連する問題