2012-05-07 34 views
2

私は新しいワインを作成するためにviewmodel(VM)を使っています。ユーザーのプロファイルに基づいてgetのvmにProducerID値を割り当てます。ビューでProducerID値がレンダリングされると、そのビューでProducerID値が表示されます。ユーザーは管理者ロール(私はそのロールでテストしていない)に属していない限り、この値を選択または編集できません。私の問題は、ProducerIDが常に0になってPOSTに戻ってくることです。ビュー内の他の選択されたオプションがうまくいけば、何が失われているのか分かりません。投稿でMVC3の値を保持していないモデル

私はvm自体に新しいユニークな名前を入れようとしましたが、それも値を保持しませんでした。私は周りを見回し、似たような問題を抱えている他の人々を見つけましたが、その解決策のどれも助けられませんでした。これについての援助は素晴らしいでしょう。ありがとう!

のviewmodel:

{ 
    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 = "Case Production")] 
    public double 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; } 

    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; } 

    public virtual ICollection<Review> Reviews { get; set; } 
    public virtual ICollection<Doc> Docs { get; set; } 

    public IEnumerable<SelectListItem> BodyList { get; set; } 

    //for dropdownlist binding 
    //public IEnumerable<VarType> VarTypes { get; set; } 
    //public IEnumerable<Origin> Origins { get; set; } 
    //public IEnumerable<App> Apps { get; set; } 
    //public IEnumerable<Vintage> Vintages { get; set; } 
    //public IEnumerable<Importer> Importers { get; set; } 
    //public IEnumerable<Producer> Producers { get; set; } 

    public Wine() 
    { 
     var BodyList = new List<SelectListItem>() 
     { 
      new SelectListItem {Value="", Text="Please select wine body"}, 
      new SelectListItem {Value="", Text="Light-bodied"}, 
      new SelectListItem {Value="", Text="Light to Medium-bodied"}, 
      new SelectListItem {Value="", Text="Medium-bodied"}, 
      new SelectListItem {Value="", Text="Medium to Full-bodied"}, 
      new SelectListItem {Value="", Text="Full-bodied"}, 
      new SelectListItem {Value="", Text="Very Full-bodied"} 
     }; 

     this.BodyList = BodyList; 
    } 

    public virtual String Name { 

     get { 
      string sName = string.Empty; 
      int iVintage; 

      if (!int.TryParse(this.Vintage.Name.Trim(), out iVintage)) 
      { 
       sName = iVintage.ToString(); 
      } 

      if (!string.IsNullOrEmpty(this.Designate)) 
      { 
       sName = sName + " " + this.Producer.Name + " " + this.Designate + " " + this.VarType.Name; 
      } 
      else 
      { 
       sName = sName + " " + this.Producer.Name + " " + this.VarType.Name; 
      } 

      return sName; 
      } 
    } 
} 

コントローラ:

public ActionResult Create() 
    { 
     NewWineViewModel nw = new NewWineViewModel(); 

     nw.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name").Default("Select a Varietal/Type", "0"); 
     nw.Origins = new SelectList(db.Origins, "OriginID", "Name").Default("Select an Origin", "0"); 
     nw.Apps = new SelectList(db.Apps, "AppID", "Name").Default("Select an Appellation", "0"); 
     nw.Vintages = new SelectList(db.Vintages, "VintageID", "Name").Default("Select a Vintage", "0"); 
     nw.Importers = new SelectList(db.Importers, "ImporterID", "Name").Default("Select an Importer", "0"); 

     // keep dynamic 
     if (User.IsInRole("producer")) 
     { 
      Producer currentProd = db.ProducerUsers.Find(Membership.GetUser().ProviderUserKey).Producer; 
      nw.Wine.ProducerID = currentProd.ProducerID; 
      ViewBag.ProducerName = currentProd.Name; 
      ViewBag.ProducerID = currentProd.ProducerID; 
     } 
     else 
     { 
      ViewBag.ProducerSelect = new SelectList(db.Producers, "ProducerID", "Name"); 
     } 

     ViewData.Model = nw; 
     return View(); 
    } 

    // 
    // POST: /Wine/Create 

    [HttpPost] 
    //[Authorize(Roles = "admin, producereditor")] 
    public ActionResult Create(NewWineViewModel nw) 
    { 
     if (ModelState.IsValid) 
     { 
      nw.Wine.Active = nw.IsRequest ? false : true; 
      nw.Wine.ImporterID = nw.Wine.ImporterID == 0 ? null : nw.Wine.ImporterID; 
      nw.Wine.CreatedBy = this.User.Identity.Name; 
      nw.Wine.CreatedOn = DateTime.Now; 
      db.Wines.Add(nw.Wine); 

      db.SaveChanges(); 

      if (nw.IsRequest) 
      { 
       nw.VOAVIRequest.WineID = nw.Wine.WineID; 
       db.VOAVIRequests.Add(nw.VOAVIRequest); 
       RedirectToAction("Requested"); 
       //redirect to "Request Submitted" page for new wines 
      } 

      return RedirectToAction("Details", nw.Wine.WineID); 
     } 

     ViewBag.VarTypeID = new SelectList(db.VarTypes, "VarTypeID", "Name").Default("Select a Varietal/Type", nw.Wine.VarTypeID.ToString()); 
     ViewBag.OriginID = new SelectList(db.Origins, "OriginID", "Name").Default("Select an Origin", nw.Wine.OriginID.ToString()); 
     ViewBag.AppID = new SelectList(db.Apps, "AppID", "Name").Default("Select an Appellation", nw.Wine.AppID.ToString()); 
     ViewBag.VintageID = new SelectList(db.Vintages, "VintageID", "Name").Default("Select a Vintage", nw.Wine.VintageID.ToString()); 
     ViewBag.ImporterID = new SelectList(db.Importers, "ImporterID", "Name").Default("Select an Importer", nw.Wine.ImporterID.ToString()); 
     if (User.IsInRole("producer")) 
     { 
      Producer currentProd = db.ProducerUsers.Find(Membership.GetUser().ProviderUserKey).Producer; 
      ViewBag.ProducerID = currentProd.ProducerID; 
      ViewBag.ProducerName = currentProd.Name; 
     } 
     else 
     { 
      ViewBag.ProducerSelect = new SelectList(db.Producers, "ProducerID", "Name" ,nw.Wine.ProducerID); 
     } 
     return View(nw); 
    } 

ビュー:

@model vf2.ViewModels.NewWineViewModel 
@{ 
    ViewBag.Title = "Create a Wine"; 
} 

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

    if (User.IsInRole("admin")) 
    { 
    <div class="editor-label"> 
     @Html.LabelFor(m => m.Wine.ProducerID, "Producer") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor(m => m.Wine.ProducerID, ViewBag.ProducerSelect as SelectList, "Select a Varietal/Type") 
     @*@Html.DropDownList("ProducerSelect", String.Empty)*@ 
    </div> 
    } 
    else 
    { 
    <h3>@ViewBag.ProducerName</h3> 
    } 
    @Html.HiddenFor(m => m.IsRequest) 
    <table> 
     <tr> 
      <td>@Html.LabelFor(m => m.Wine.VarTypeID, "VarType") 
      </td> 
      <td> 
       <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, "Origin") 
      </td> 
      <td> 
       <div class="voavi-select"> 
        @Html.DropDownListFor(m => m.Wine.OriginID, Model.Origins, new { @class = "chzn-select" }) 
       </div> 
       <a id="lnkNewOrigin" class="filetypes" href="#">New Origin?</a> 
       @Html.TextBoxFor(m => m.VOAVIRequest.Origin, new { style = "display: none;", @class = "voavignore" }) 
      </td> 
     </tr> 
     <tr> 
      <td> 
       @Html.LabelFor(m => m.Wine.AppID, "App") 
      </td> 
      <td> 
       <div class="voavi-select"> 
        @Html.DropDownListFor(m => m.Wine.AppID, Model.Apps, new { @class = "chzn-select" }) 
       </div> 
       <a id="lnkNewApp" class="filetypes" href="#">New Varietal?</a> 
       @Html.TextBoxFor(m => m.VOAVIRequest.App, new { style = "display: none;", @class = "voavignore" }) 
      </td> 
     </tr> 
     <tr> 
      <td> 
       @Html.LabelFor(m => m.Wine.VintageID, "Vintage") 
      </td> 
      <td> 
       <div class="voavi-select"> 
        @Html.DropDownListFor(m => m.Wine.VintageID, Model.Vintages, new { @class = "chzn-select" }) 
       </div> 
       <a id="lnkNewVintage" class="filetypes" href="#">New Varietal?</a> 
       @Html.TextBoxFor(m => m.VOAVIRequest.Vintage, new { style = "display: none;", @class = "voavignore" }) 
      </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.Body) 
      </td> 
      <td> 
       @Html.DropDownListFor(m => m.Wine.Body, new SelectList(Model.Wine.BodyList, "Value", "Text"), new { @class = "chzn-select" }) 
      </td> 
     </tr> 
     <tr> 
      <td> 
       @Html.LabelFor(m => m.Wine.ImporterID, "Importer") 
      </td> 
      <td> 
       <div class="voavi-select"> 
        @Html.DropDownListFor(m => m.Wine.ImporterID, Model.Importers, new { @class = "chzn-select" })</div> 
       <a id="lnkNewImporter" class="filetypes" href="#">New Varietal?</a> 
       @Html.TextBoxFor(m => m.VOAVIRequest.Importer, new { style = "display: none;" }) 
      </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.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="Create" /> 
    </p> 
} 
+0

ViewData.Model = nw;ビューを返します。それはreturn View(nw)と同じです。 ? – kenny

答えて

6

ProducerIDは、フォームでポストバックされていないように見えます。ルートに含まれていない場合は、非表示のフィールドにそれを保持する必要があります。

@Html.HiddenFor(m => m.ProducerID) 
+0

ありがとうございました。とにかく私はルートを使ってそれを合理化することができますか? – user576838

+0

+1ありがとうricheym;完璧な&非常に論理的に働いた。 – Catto

関連する問題