【问题标题】:DropdownListFor Returns null after HTTP PostBackDropdownListFor 在 HTTP PostBack 之后返回 null
【发布时间】:2015-05-29 14:18:41
【问题描述】:

应用程序的上下文是维护来自投资顾问的安全订单。在用户修改订单的屏幕上出现了问题。在这样的屏幕中,我有一个下拉列表来指定订单类型是买入还是卖出,并显示安全性、数量和价格的值。

问题 在进行修改后,我在编辑屏幕中目睹了(测试不是通过更改买入/卖出,而是通过其他方式进行,即价格)。如果我执行 HTTP Post,DropDownList 的值将返回 null。参考截图:

SelectList类型的初始化

public static List<SelectListItem> getBuySellList()
        {
            List<SelectListItem> buySell = new List<SelectListItem>();

            SelectListItem item;

            item = new SelectListItem();
            item.Text = "BUY";
            item.Value = "BUY";
            buySell.Add(item);

            item = new SelectListItem();
            item.Text = "SELL";
            item.Value = "SELL";
            buySell.Add(item);

            return buySell;
        }

我的控制器如下:

// GET: OrderFlow/Edit/5
        public ActionResult Edit(int id)
        {
            OrderFlowModel orderFlowModel = db.Find(id);

            ViewData["ORDERFLOW_NO"] = id;
            ViewBag.OrderFlowBuySell = Utility.UtilityDBContext.getBuySellList();

            return View(orderFlowModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(string OrderFlowNo, string OrderFlowSecurityID, string OrderFlowBuySell, string OrderFlowQuantity, string OrderFlowPrice, string OrderFlowTradingDate, string OrderFlowClientAccount, string OrderFlowParticipant, string OrderFlowBuyStatus)
        {
            if (ModelState.IsValid)
            {

                OrderFlowModel orderFlowModel = new OrderFlowModel();
                orderFlowModel.OrderFlowNo = int.Parse(OrderFlowNo.ToString());
                orderFlowModel.EquityID = OrderFlowSecurityID;
                orderFlowModel.BuySell = OrderFlowBuySell;
                orderFlowModel.Quantity = int.Parse(OrderFlowQuantity);
                orderFlowModel.Price = double.Parse(OrderFlowPrice);

                DateTime dt;
                if (DateTime.TryParseExact(OrderFlowTradingDate, formats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                {
                    orderFlowModel.TradingDate = dt;
                }
                else orderFlowModel.TradingDate = DateTime.Today;

                orderFlowModel.ClientAccountID = OrderFlowClientAccount;
                orderFlowModel.ParticipantAccountID = OrderFlowParticipant;
                orderFlowModel.Status = OrderFlowBuyStatus;

                try
                {
                    db.Edit(orderFlowModel);
                    return RedirectToAction("Index");
                }
                catch (Exception er)
                {
                    TempData["Message"] = er.Message;
                }

            }

            ViewBag.OrderFlowBuySell = Utility.UtilityDBContext.getBuySellList();
            return RedirectToAction("Edit", new{id=OrderFlowNo});
        }

订单流模型:

public class OrderFlowModel
    {
        [Display(Name = "Order Flow No")]
        public int OrderFlowNo { get; set; }

        [Display(Name = "Valid Till")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        [DataType(DataType.Date)]
        public DateTime TradingDate { get; set; }

        [Display(Name = "Client A/c ID")]
        public string ClientAccountID { get; set; }

        [Display(Name = "Participant ID")]
        public string ParticipantAccountID { get; set; }

        [Required(ErrorMessage="Security is Required")]
        [Display(Name = "Security")]
        public string EquityID { get; set; }

        [Required(ErrorMessage = "Buy or Sell Needs to specify")]
        [Display(Name = "BS")]
        public string BuySell { get; set; }

        [DefaultSettingValue("0")]
        [Display(Name = "Quantity")]
        [DisplayFormat(DataFormatString = "{0:N0}")]
        public int Quantity { get; set; }

        [Display(Name = "Price")]
        [DataType(DataType.Currency)]
        [DisplayFormat(DataFormatString = "{0:N2}")]
        public double Price { get; set; }

        [Display(Name = "Status")]
        public string Status { get; set; }

        [Display(Name = "User Entered")]
        public string UserEntered { get; set; }

        [Display(Name = "Effective From")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime EffectiveStart { get; set; }

        [Display(Name = "Effective Till")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime EffectiveEnd { get; set; }
    }

我在 Razor 中分​​配 DropdownListFor 的方式如下:

@Html.DropDownListFor(model => model.BuySell, new SelectList(ViewBag.OrderFlowBuySell, "Text", "Value"), new { @id = "OrderFlowBuySell", @class = "form-control" })

下拉列表的浏览器的 HTML 输出

<select class="form-control" data-val="true" data-val-required="Buy or Sell Needs to specify" id="OrderFlowBuySell" name="BuySell"><option selected="selected" value="BUY">BUY</option>
<option value="SELL">SELL</option>
</select>

【问题讨论】:

    标签: c# asp.net asp.net-mvc razor asp.net-mvc-5


    【解决方案1】:

    需要在你的控制器方法中的值是BuySell,这是从下面的标记中选择的下拉列表的ID(第一个参数):

    @Html.DropDownListFor(model => model.BuySell, 
           new SelectList(ViewBag.OrderFlowBuySell, "Text", "Value"), 
           new { @id = "OrderFlowBuySell", @class = "form-control" })
    

    OrderFlowBuySell 是用于绑定下拉菜单的选项集合,在帖子中您通常只关心用户选择的选项。

    把它改成这个,值就会被贴出来:

    Edit(string OrderFlowNo, string OrderFlowSecurityID, 
        string OrderFlowBuySell, string OrderFlowQuantity, 
        string OrderFlowPrice, string OrderFlowTradingDate, 
        string OrderFlowClientAccount, string OrderFlowParticipant, 
        string OrderFlowBuyStatus, string BuySell)
    

    但是我强烈建议您使用 ViewModels,这样您就可以将单个对象指定给您的控制器帖子。

    【讨论】:

    • @huthonoid 谢谢 非常感谢它解决了。我从 BuySell 签名中获得价值。 :) 对于其他人:我更改了我的 OrderFlow 控制器的编辑签名并添加了 string BuySell,正如 Hutch 提到的那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2016-05-06
    • 1970-01-01
    • 2019-08-13
    相关资源
    最近更新 更多