0

私はSOを見回しましたが、このような問題を抱えている人は誰も見つからないので、この質問を投稿しています。

コントローラに2つの編集アクション(postとget)があり、GETでは、ビューに渡されるビューモデルのWineプロパティにデータを入力します。

POSTで、そのビューモデルを読み取り、変更をデータベースに保存します。その後、更新されたWineプロパティを使用してLucene検索インデックスを更新したいと思いました。ただし、Wineプロパティにはナビゲーションプロパティが設定されていないため、エラーが発生し続けます。

db.Wines.Find(ew.wine.WindID)を使用して新しいwineオブジェクトを検索しようとしましたが、それでもナビゲーションプロパティのないWineが返されます。これはまさにGET編集アクションが行うことであり、が正しく入力されていることを言及する価値があります。ここで何が起こっているのかわかりません。また、Wine w = db.Entry(ew.Wine).Entityを試して、これらのナビゲーションプロパティを設定しましたが、役に立ちませんでした...事前に助けてくれてありがとう!

ViewModel&Wine Model

    public class NewWineViewModel
    {

        public Wine Wine { get; set; }
        public VOAVIRequest VOAVIRequest { get; set; }
        public bool IsRequest { get; set; }

        public SelectList VarTypes { get; set; }
        public SelectList Origins { get; set; }
        public SelectList Apps { get; set; }
        public SelectList Vintages { get; set; }
        public SelectList Importers { get; set; }

        public NewWineViewModel()
        {
            this.Wine = new Wine();
        }

    }

 public class Wine :Updater
    {
        public int WineID { get; set; }
        //public int WineTypeID { get; set; }
        [Display(Name = "Varietal/Type")]
        public int? VarTypeID { get; set; }
        [Display(Name = "Origin")]
        public int? OriginID { get; set; }
        [Display(Name = "Appellation")]
        public int? AppID { get; set; }
        [Display(Name = "Vintage")]
        public int? VintageID { get; set; }
        [Display(Name = "Importer")]
        public int? ImporterID { get; set; }
        public int ProducerID { get; set; }
        public string Designate { get; set; }
        [Display(Name = "Drink Window")]
        public string DrinkWindow { get; set; }
        public string Body { get; set; }
        public string SKU { get; set; }
        [Display(Name = "Varietal Makeup")]
        public string VarietalMakeup { get; set; }
        [Display(Name = "Case Production")]
        public string CaseProduction { get; set; }
        [Display(Name = "Alcohol Content")]
        public double? AlcoholContent { get; set; }
        public string Winemaker { get; set; }
        [Display(Name = "Consulting Winemaker")]
        public string ConsultWinemaker { get; set; }
        public bool Sustainable { get; set; }
        public bool Kosher { get; set; }
        public bool Organic { get; set; }
        public bool Biodynamic { get; set; }
        public bool SalmonSafe { get; set; }
        public Boolean Active { get; set; }
        [Display(Name = "ResidualSugar")]
        public double? RS { get; set; }
        public double? pH { get; set; }
        public string QRUrl { get; set; }


        public virtual WineType WineType { get; set; }

        public virtual VarType VarType { get; set; }
        public virtual Origin Origin { get; set; }
        public virtual App App { get; set; }
        public virtual Vintage Vintage { get; set; }
        public virtual Importer Importer { get; set; }
        public virtual Producer Producer { get; set; }
}

コントローラからの2つの編集アクション:

 [ProducerEdit]
    public ActionResult Edit(int WineID)
    {

        NewWineViewModel ew = new NewWineViewModel();
        ew.Wine = db.Wines.Find(WineID);
        ew.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name", ew.Wine.VarTypeID);
        ew.Origins = new SelectList(db.Origins, "OriginID", "Name", ew.Wine.OriginID);
        ew.Apps = new SelectList(db.Apps, "AppID", "Name", ew.Wine.AppID);
        ew.Vintages = new SelectList(db.Vintages, "VintageID", "Name", ew.Wine.VintageID);
        //might need to handle null on this one
        ew.Importers = new SelectList(db.Importers, "ImporterID", "Name", ew.Wine.ImporterID);
        return View(ew);

    }

    //post
    [HttpPost]
    [ProducerEdit]
    public ActionResult Edit(NewWineViewModel ew)
    {

        if (ModelState.IsValid)
        {
            ew.Wine.Body = ew.Wine.Body == "Please select wine body" ? string.Empty : ew.Wine.Body;

            ew.Wine.UpdatedBy = User.Identity.Name;
            ew.Wine.UpdatedOn = DateTime.Now;
            db.Entry(ew.Wine).State = EntityState.Modified;
            db.SaveChanges();

            Wine w = db.Wines.Find(ew.Wine.WineID);


            Lucene.LuceneSearch.ClearLuceneIndexRecord(ew.Wine.WineID);
            Lucene.LuceneSearch.AddUpdateLuceneIndex(ew.Wine);

            if (ew.IsRequest)
            {
                ew.VOAVIRequest.WineID = ew.Wine.WineID;
                ew.VOAVIRequest.CreatedBy = User.Identity.Name;
                ew.VOAVIRequest.CreatedOn = DateTime.Now;
                db.VOAVIRequests.Add(ew.VOAVIRequest);
                db.SaveChanges();

                return RedirectToAction("Requested");

                //redirect to "Request Submitted" page for new wines
            }

            return RedirectToAction("Details", new { id = ew.Wine.WineID });

        }
        else
        {
            ew.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name", ew.Wine.VarTypeID);
            ew.Origins = new SelectList(db.Origins, "OriginID", "Name", ew.Wine.OriginID);
            ew.Apps = new SelectList(db.Apps, "AppID", "Name", ew.Wine.AppID);
            ew.Vintages = new SelectList(db.Vintages, "VintageID", "Name", ew.Wine.VintageID);
            //might need to handle null on this one
            ew.Importers = new SelectList(db.Importers, "ImporterID", "Name", ew.Wine.ImporterID);
            return View(ew);
        }

    }

編集-これがRazorのフォームです

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <h3>@ViewBag.ProducerName</h3>
    @Html.HiddenFor(m => m.Wine.WineID)
    @Html.HiddenFor(m => m.Wine.ProducerID)
    @Html.HiddenFor(m => m.IsRequest)

    <h4 style="margin-top: -40px; padding-bottom: 10px;">Editing @Model.Wine.Name</h4>
    <table>
        <tr>
            <td>@Html.LabelFor(m => m.Wine.VarTypeID)
            </td>
            <td style="display: inline">
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.VarTypeID, Model.VarTypes, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.VarType, new { style = "display: none;", @class = "voavignore" })
                <a id="lnkNewVar" class="filetypes" href="#">New Varietal?</a> @*                @Html.ValidationMessageFor(m => m.VOAVIRequest.VarType)*@
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.OriginID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.OriginID, Model.Origins, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.Origin, new { style = "display: none;", @class = "voavignore" })
                <a id="lnkNewOrigin" class="filetypes" href="#">New Origin?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.AppID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.AppID, Model.Apps, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.App, new { style = "display: none;", @class = "voavignore" })<a
                    id="lnkNewApp" class="filetypes" href="#">New Varietal?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.VintageID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.VintageID, Model.Vintages, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.Vintage, new { style = "display: none;", @class = "voavignore" })
                <a id="lnkNewVintage" class="filetypes" href="#">New Varietal?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Designate)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Designate)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.DrinkWindow)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.DrinkWindow)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.VarietalMakeup)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.VarietalMakeup)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Body)
            </td>
            <td>
                @Html.DropDownListFor(m => m.Wine.Body, new SelectList(Model.Wine.BodyList, "Text", "Text", Model.Wine.Body), new { @class = "chzn-select" })
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.ImporterID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.ImporterID, Model.Importers, new { @class = "chzn-select" })</div>
                @Html.TextBoxFor(m => m.VOAVIRequest.Importer, new { style = "display: none;" })
                <a id="lnkNewImporter" class="filetypes" href="#">New Varietal?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.SKU)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.SKU)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.CaseProduction)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.CaseProduction)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.AlcoholContent)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.AlcoholContent)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.RS)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.RS)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.pH)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.pH)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Winemaker)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Winemaker)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.ConsultWinemaker)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.ConsultWinemaker)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Sustainable)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Sustainable)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Kosher)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Kosher)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Organic)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Organic)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Biodynamic)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Biodynamic)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.SalmonSafe)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.SalmonSafe)
            </td>
        </tr>
    </table>
    <p>
        <input type="submit" value="Update" />
    </p>
}
4

0 に答える 0