c# - DropdownListFor Returns null after HTTP PostBack -
the context of application maintaining security orders investment advisors. on screen users amends orders problem appears. in such screen have dropdown list specify order type whether buy or sell , shows values security, quantity , price.
problem have witnessed while being in edit screen, after doing amendment (tests have performed not changing buy/sell others i.e price). if performed http post, values of dropdownlist returns null. refer screenshot:
initialization of selectlist
type
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; }
my controller follows:
// 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}); }
the orderflow model:
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 required")] [display(name = "security")] public string equityid { get; set; } [required(errormessage = "buy or sell needs 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; } }
the way have assigned dropdownlistfor in razor follows:
@html.dropdownlistfor(model => model.buysell, new selectlist(viewbag.orderflowbuysell, "text", "value"), new { @id = "orderflowbuysell", @class = "form-control" })
html output browser dropdown list
<select class="form-control" data-val="true" data-val-required="buy or sell needs specify" id="orderflowbuysell" name="buysell"><option selected="selected" value="buy">buy</option> <option value="sell">sell</option> </select>
the value needs in controller method buysell
, selected id of dropdownlist mark-up below (the first parameter):
@html.dropdownlistfor(model => model.buysell, new selectlist(viewbag.orderflowbuysell, "text", "value"), new { @id = "orderflowbuysell", @class = "form-control" })
the orderflowbuysell
collection of options used bind dropdown, in post concerned option user has selected.
change , value posted:
edit(string orderflowno, string orderflowsecurityid, string orderflowbuysell, string orderflowquantity, string orderflowprice, string orderflowtradingdate, string orderflowclientaccount, string orderflowparticipant, string orderflowbuystatus, string buysell)
however advise use viewmodels, way can speficy single object controller post.
Comments
Post a Comment