2

我正在使用视图模型(vm)来创建新酒。我根据用户的配置文件将 ProducerID 值分配给 get 上的 vm。当它在视图中呈现时,我可以在视图中看到 ProducerID 值。用户无法选择或编辑此值,除非他们处于管理员角色(我没有使用该角色进行测试)。我的问题是 ProducerID 在 POST 上总是返回为 0。我不知道我缺少什么,因为我在视图中选择的其他选项恢复正常。

我试图在 vm 本身中添加一个新的唯一名称,但这也没有任何价值。我环顾四周,发现其他一些人也有类似的问题,但他们的解决方案都没有帮助。对此的任何帮助都会很棒。谢谢!

视图模型:

{
    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>
}
4

1 回答 1

6

ProducerID没有被填充,因为它看起来没有与表单一起发回。如果它不属于您的路线,则需要将其保存在隐藏字段中:

@Html.HiddenFor(m => m.ProducerID)
于 2012-05-07T00:57:07.770 回答