Answered by:
dropdownlist value db

Question
-
User-265772647 posted
Hi,
Im trying to store dropdown value in database but it was taking the value not the text.
model
public string Entitlement { get;set; }
public static IEnumerable GetEntitlement()
{
List<SelectListItem> EntitlementOptions = new List<SelectListItem>(){
new System.Web.Mvc.SelectListItem()
{
Text="T Officer",
Value = "1"
},
new System.Web.Mvc.SelectListItem()
{
Text="Customer Officer",
Value = "2"
},
};return EntitlementOptions;
}html
<div class="form-group">
@Html.LabelFor(model => model.Entitlement, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Entitlement, (List<SelectListItem>)ViewBag.EntitlementOptions, "--Select Entitlement--")
@*@Html.EditorFor(model => model.Entitlement, new { htmlAttributes = new { @class = "form-control" } })*@@Html.ValidationMessageFor(model => model.Entitlement, "", new { @class = "text-danger" })
</div>
</div>Controller:
[HttpPost]
public ActionResult NonAdminUser(NonAdminUser nonAdmin)
{
string mainconn = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
SqlConnection con = new SqlConnection(mainconn);
string sqlQuery = "insert into NonAdminUser values(@Entitlement)";
con.Open();
SqlCommand cmd = new SqlCommand(sqlQuery, con);cmd.Parameters.AddWithValue("@Entitlement", nonAdmin.Entitlement);
cmd.ExecuteNonQuery();
con.Close();
//return View();return View("Success");
}Thursday, May 21, 2020 6:06 PM
Answers
-
User475983607 posted
emaak
im trying to insert text in database .
In HTML forms name/value pairs are submitted. If you want the value to be text then set the value attribute to the text rather than the ID.
Usually, you want to insert IDs. Then use a JOIN to get the text.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 21, 2020 7:21 PM
All replies
-
User475983607 posted
emaak
Im trying to store dropdown value in database but it was taking the value not the text.Right. That's how HTML forms work. Why do you need the text if you have the ID that uniquely identifies the text?
Thursday, May 21, 2020 6:59 PM -
User-265772647 posted
im trying to insert text in database .
Thursday, May 21, 2020 7:07 PM -
User475983607 posted
emaak
im trying to insert text in database .
In HTML forms name/value pairs are submitted. If you want the value to be text then set the value attribute to the text rather than the ID.
Usually, you want to insert IDs. Then use a JOIN to get the text.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 21, 2020 7:21 PM