0

MVCでEFを使用するマスター詳細シナリオで使用するために、関連データに頭を悩ませようとしています。StackOverflowを検索することで、かなりの部分を見つけることができましたが、データの保存中に問題が発生しました。StackOverflowのその他の例として、個人/住所の設定を使用します。個人と住所(1対多)には、ビューのエンティティプロパティの一部のみを使用します。デフォルトのスキャフォールドでは、これによりエラーが発生しますが、personエンティティの場合、以下のコードを使用してそれを解決することができました。しかし、Addressエンティティの場合、同じエラーが発生し、これを解決する方法がわかりません。これが私がこれまでに得たものです:

ContactController:

public ActionResult Edit(int id)
{
    Contact contact = db.Contacts
        .Include(c => c.Address)
        .Include(c => c.Relation)
        .Where(c => c.ContactId == id)
        .Single();
    return View(contact);
}


[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
    var contactToUpdate = db.Contacts
        .Include(c => c.Address)
        .Include(c => c.Relation)
        .Where(c => c.ContactId == id)
        .Single();

    if (TryUpdateModel(contactToUpdate, "", null, new string[] { "Relation" }))
    {
        try
        {
            db.Entry(contactToUpdate).State = EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");

        }
        catch (DataException ex)
        {
            ModelState.AddModelError("", "Error while saving.");
            return View();
        }
    }
    return View(contactToUpdate); 
}

私のモデルのクラス:

namespace MyApp.Models
{
    public class Contact
    {
        public int ContactId { get; set; }
        public string Weergavenaam { get; set; }

        public virtual ICollection<Address> Address{ get; set; }
        public virtual ICollection<Relation> Relation { get; set; }
    }

    public class Address
    {
        public int AddressId { get; set; }
        public string Street{ get; set; }
        public string Postal{ get; set; }
        public string Place{ get; set; }
        public string State{ get; set; }

        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }

        public virtual Contact Contact { get; set; }
    }

    public class Relation
    {
        ...
    }
}

ContactControllerの編集アクションのビュー:

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Contact</legend>

    @Html.HiddenFor(model => model.ContactId)

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    @Html.EditorFor(model => model.Address) 


    <div class="display-field">
        Created: @Html.DisplayFor(model => model.Created)
    </div>
    <div class="display-field">
        Modified: @Html.DisplayFor(model => model.Modiied)
    </div>

    <p><input type="submit" value="Save" /></p>
</fieldset>
}

アドレスモデルのEditorTemplate

@model MyApp.Models.Address
<fieldset>
    <legend>Address</legend>

    @Html.HiddenFor(model => model.AddressId)

    <div class="editor-label">
        @Html.LabelFor(model => model.Street)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Street)
        @Html.ValidationMessageFor(model => model.Street)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Postal)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Postal)
        @Html.ValidationMessageFor(model => model.Postal)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Place)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Place)
        @Html.ValidationMessageFor(model => model.Place)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.State)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.State)
        @Html.ValidationMessageFor(model => model.State)
    </div>
</fieldset>
4

1 に答える 1

0

コードで少し苦労した後、問題を解決する唯一の方法は、@Html.HiddenFor を使用して不足しているプロパティをエディター テンプレートに追加することだと考えました。もちろん、私の状況を機能させるためのより洗練された方法があるはずですが、今のところそれで問題は解決します。

この問題に対するより良い解決策は大歓迎です!

于 2012-07-18T06:42:17.217 に答える