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