asp.net-mvc - 模型在 Post 上的 MVC3 中不保留值(value)

标签 asp.net-mvc asp.net-mvc-3 viewmodel

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

我试图在虚拟机本身中放置一个新的唯一名称,但它也没有任何值(value)。我环顾四周,发现其他一些人也有类似的问题,但他们的解决方案都没有帮助。在这方面的任何帮助都会很棒。谢谢!

View 模型:

{
    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();
    }
}

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

Controller :

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

最佳答案

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

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

关于asp.net-mvc - 模型在 Post 上的 MVC3 中不保留值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10475469/

相关文章:

asp.net-mvc - 使用 CheckBoxFor

c# - Asp.net MVC - 在 cshtml 页面中创建环境特定的元标记

asp.net-mvc - asp.net mvc 模型与 Entity Framework 模型

ios - 在iOS中使用MVVM时如何在不同片段之间进行通信

c# - ASP NET MVC 3 - 如何首先使用两个表和 Database.Setinitializer 在代码中重置数据库?

c# - Html.CheckBoxFor 所需的复选框验证工作相反

javascript - HTML 表格和数据表对齐

jquery - Ajax 调用从 MVC3 Controller 获取 Json

wpf - 为什么 ViewModel 需要实现 INotifyPropertyChanged 或使用依赖属性?

asp.net-mvc - 我可以像使用 xslt-renderings 一样在 Sitecore 中使用 Razor View 吗?