问题
I have an action method, called Index_shift
inside one my controller:
public ActionResult Index_shift()
{
ViewBag.shift_details = new SelectList(Getshift_details(),"ShiftId","ShiftVal");
return View();
}
The Getshit_details
implementation:
private List<shift_details> Getshift_details()
{
List<shift_details> shift_detail = new List<shift_details>();
shift_detail.Add(new shift_details { ShiftId = 0, ShiftVal = "Select" });
shift_detail.Add(new shift_details { ShiftId = 1, ShiftVal = "A" });
shift_detail.Add(new shift_details { ShiftId = 2, ShiftVal = "B" });
shift_detail.Add(new shift_details { ShiftId = 3, ShiftVal = "C" });
shift_detail.Add(new shift_details { ShiftId = 4, ShiftVal = "G" });
return shift_detail;
}
The action is called from the following view section:
@using (Html.BeginForm("Index_shift", "Scm_Mod_Sug", FormMethod.Post))
{
<div>
@Html.DropDownList("shift_details", ViewBag.shift_details as SelectList, new { Class = "form-control selectpicker show-tick" })
<input id="btnLogin" type="submit" value="Submit" class="btn btn-primary btn-block" />
</div>
}
The return value of the http method is the value, which I pass (0, 1, 2, 3, 4), not the shift value (Select, A, B, C, D, G).
The code, which communicates with the database:
public ActionResult Index_shift(FormCollection form)
{
string shift_details = form["shift_details"];
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["ABC"].ToString());
try
{
conn.Open();
OracleCommand command = new OracleCommand();
command.Connection = conn;
command.CommandText = "pd01";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("V_SHIFT", OracleDbType.Varchar2).Value = shift_details;
}
//...
}
I am getting the value 1 in the shift details. I want to pass value '1' and receive 'A' in return.
Any idea how can I achieve it are highly appreciated.
回答1:
Use
new SelectList(ViewBag.shift_details, "ShiftId", "ShiftVal ")
Instead of
ViewBag.shift_details as SelectList
来源:https://stackoverflow.com/questions/62632108/trying-to-get-key-value-pass-in-the-proc-in-mvc