申请的背景是维护投资顾问的安全订单。在用户修改其订单的屏幕上出现问题。在这样的屏幕中,我有下拉列表来指定订单类型,无论是买入还是出售,并显示安全性,数量和价格的值。
问题 在进行修改后,我在编辑屏幕中亲眼目睹了(测试不是通过更改买/卖而是其他,即价格)。如果我执行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; }
}
我在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>
答案 0 :(得分: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,这样您就可以将单个对象设置为控制器帖子。