1
應用程序的上下文是維護來自投資顧問的安全訂單。在用戶修改他的訂單的屏幕上出現問題。在這樣的屏幕中,我有下拉列表來指定訂單類型,無論是買入還是賣出,並顯示安全,數量和價格的值。DropdownListFor在HTTP回傳後返回空
問題 同時在編輯界面是我親眼目睹,做一個修改後(測試已通過改變購買/賣出但其他人即價格進行不)。如果我執行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});
}
的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; }
}
我在剃刀分配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>
@huthonoid非常感謝你解決了。我從BuySell簽名獲得Value。 :)對於其他人:我改變了我的OrderFlow Controller的編輯簽名,並在Hutch提到的時候添加了'string BuySell'。 – hiFI
@hiFI很好,非常感謝。 :) – hutchonoid