c# - System.Web.Mvc.SelectListItem cannot be used as a parameter value -
i using mvc 5 problem in insertion dropdownlist selected list
my view:
<div class="form-group"> @html.labelfor(m => m.natureofleave, new { @class = "control-label col-sm-6" }) <div class="col-sm-6 pushleft"> @html.dropdownlistfor(m => m.natureofleave, viewbag.employeeleave ienumerable<selectlistitem>, "-select-") @html.validationmessagefor(m => m.natureofleave, "", new { @style = "color:red;" }) </div> </div>
my model:
[display(name = "type of leave")] public list<selectlistitem> natureofleave { get; set; }
my controller:
[httppost] [allowanonymous] [validateantiforgerytoken] public actionresult leaves(employeeleave objempleave) { objempleave.natureofleave = binddatafromdatabasetoleave(); var selectitem = objempleave.natureofleave.find(p => p.value == objempleave.natureid.tostring()); if (selectitem != null) { selectitem.selected = true; viewbag.message = "natureofleave " + selectitem.text; } if (string.isnullorempty(objempleave.startdate.tostring())) { modelstate.addmodelerror("error", "please provide start date"); } else if (string.isnullorempty(objempleave.enddate.tostring())) { modelstate.addmodelerror("error", "end date calculated startdate... so, make sure start date not empty!"); } else if (string.isnullorempty(objempleave.natureofleave.tostring())) { modelstate.addmodelerror("error", "select one"); } else if (string.isnullorempty(objempleave.reason)) { modelstate.addmodelerror("error", "reason cannot empty"); } else { objempleave.userid = convert.toint32(session["userid"]); objempleave.username = convert.tostring(session["name"]); int leaveid = objiaccountdata.insertleave(objempleave); if (leaveid > 0) { tempdata["msg"] = "<script>alert('your application has been sent')</script>"; } else { tempdata["msg"] = "<script>alert('something went wrong send application')</script>"; } } return view(objempleave); }
i using dapper orm give repository.
my repository:
using (sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["mystring"].tostring())) { var param = new dynamicparameters(); param.add("@userid", objemployeeleave.userid); param.add("@username", objemployeeleave.username); param.add("@startdate", objemployeeleave.startdate); param.add("@enddate", objemployeeleave.enddate); param.add("@leavesavailed", objemployeeleave.leavesavailed); param.add("@natureofleave", objemployeeleave.natureofleave); param.add("@reason", objemployeeleave.reason); param.add("@useridout", dbtype: dbtype.int32, direction: parameterdirection.output); con.open(); con.execute("sprocinsertleaves", param, null, 0, commandtype.storedprocedure); int useridout = param.get<int>("useridout"); con.close(); return useridout; }
actually, problem in repository located @ dropdownlist param.add("@natureofleave", objemployeeleave.natureofleave);
so, can solve problem.... in advance
based on understanding
first need fill natureofleave not employeeleave
second in case have model in page need no viewbag
third not filling viewbag employeeleave
this code:
return view(objempleave);
is filling model , not viewbag
why not use below if declaring model @ top of view page since model employeeleave:
@html.dropdownlistfor(m => m.natureofleave, model.natureofleave, "-select-")
here better use:
configurationmanager.connectionstrings["mystring"].connectionstring
since using:
using (sqlconnection con = new sqlconnection( configurationmanager.connectionstrings["mystring"].tostring()))
no need @ to:
con.close();
since dispose close , clean since dispose calling close inside it
last thing it's recommended to:
try not use viewbag unless forced to
for inserting multiple data:
read how work tables relation 1 (named parent) => many (named child)
to insert in parent command insert in child
Comments
Post a Comment