Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTTP回发后DropdownListFor返回null_C#_Asp.net_Asp.net Mvc_Razor_Asp.net Mvc 5 - Fatal编程技术网

C# HTTP回发后DropdownListFor返回null

C# HTTP回发后DropdownListFor返回null,c#,asp.net,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Razor,Asp.net Mvc 5,应用程序的上下文是维护来自投资顾问的安全订单。在用户修改订单的屏幕上,问题出现了。在这样的屏幕中,我有一个下拉列表来指定订单类型是买入还是卖出,并显示安全性、数量和价格的值 问题 在进行修改后,我在编辑屏幕上目睹了这一过程(测试不是通过更改买入/卖出,而是通过其他方式进行的,即价格)。如果执行HTTP Post,DropDownList的值将返回null。请参阅屏幕截图: 初始化选择列表类型 public static List<SelectListItem> getBuySell

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

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

初始化
选择列表
类型

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;
        }
OrderFlow模型:

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的方式如下:

// 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});
        }
@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>
购买
卖

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

OrderFlowBuySell
是用于绑定下拉列表的选项集合,在文章中,您通常只关注用户选择的选项

将其更改为此,值将被过账:

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

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

@huthonoid非常感谢您解决了这个问题。我从买卖签名中获得价值。:)对于其他人:我更改了OrderFlow控制器的编辑签名,并添加了Hutch提到的
字符串BuySell
Edit(string OrderFlowNo, string OrderFlowSecurityID, 
    string OrderFlowBuySell, string OrderFlowQuantity, 
    string OrderFlowPrice, string OrderFlowTradingDate, 
    string OrderFlowClientAccount, string OrderFlowParticipant, 
    string OrderFlowBuyStatus, string BuySell)